/* ── Reset & Variables ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --sidebar-bg: #1a1d2e;
  --sidebar-hover: #252840;
  --sidebar-active: #2f3352;
  --text: #2d3142;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --border-light: #f0f1f3;
  --primary: #4f6ef7;
  --primary-hover: #3b5de7;
  --primary-light: #eef2ff;
  --success: #10b981;
  --success-light: #ecfdf5;
  --warning: #f59e0b;
  --warning-light: #fffbeb;
  --error: #ef4444;
  --error-light: #fef2f2;
  --info: #6366f1;
  --info-light: #eef2ff;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,.05), 0 2px 4px rgba(0,0,0,.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.08);
  --transition: 150ms ease;
  --sidebar-w: 240px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

html { font-size: 14px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--sidebar-bg);
  color: #c8cad0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 20px 24px;
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.brand-icon {
  font-size: 28px;
  color: var(--primary);
  line-height: 1;
}
.brand-name {
  font-size: 17px;
  font-weight: 700;
  color: #fff;
  letter-spacing: .3px;
}
.brand-sub {
  font-size: 11px;
  color: #8b8fa4;
  margin-top: 1px;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: 6px;
  color: #a0a4b8;
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
  transition: all var(--transition);
}
.nav-item:hover { background: var(--sidebar-hover); color: #d4d6e0; }
.nav-item.active { background: var(--sidebar-active); color: #fff; }
.nav-item svg { opacity: .7; flex-shrink: 0; }
.nav-item.active svg { opacity: 1; }

.nav-section {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #5a5e74;
  padding: 18px 14px 6px;
}

.api-key-display {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg);
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid var(--border);
  word-break: break-all;
  user-select: all;
}

.config-editor {
  font-family: var(--font-mono);
  font-size: 12px;
  min-height: 120px;
  resize: vertical;
}

.tool-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  transition: box-shadow var(--transition);
}
.tool-card:hover { box-shadow: var(--shadow-md); }
.tool-name { font-weight: 650; font-size: 14px; margin-bottom: 4px; }
.tool-desc { font-size: 13px; color: var(--text-secondary); }
.tool-id { font-family: var(--font-mono); font-size: 11px; color: var(--text-muted); }

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid rgba(255,255,255,.07);
}
.server-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}
.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background var(--transition);
}
.status-dot.online { background: var(--success); box-shadow: 0 0 6px rgba(16,185,129,.4); }
.status-text { color: #8b8fa4; }

/* ── Main ── */
.main {
  flex: 1;
  margin-left: var(--sidebar-w);
  padding: 28px 32px;
  min-height: 100vh;
}

/* ── Page Header ── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}
.page-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}
.page-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}
.page-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ── Cards / Stat Cards ── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  transition: box-shadow var(--transition);
}
.stat-card:hover { box-shadow: var(--shadow-md); }
.stat-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
}
.stat-card.primary .stat-value { color: var(--primary); }
.stat-card.success .stat-value { color: var(--success); }
.stat-card.warning .stat-value { color: var(--warning); }
.stat-card.error .stat-value { color: var(--error); }

/* ── Card / Panel ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-title {
  font-size: 14px;
  font-weight: 650;
  color: var(--text);
}
.card-body { padding: 20px; }
.card-body.no-pad { padding: 0; }

/* ── Table ── */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
thead th {
  text-align: left;
  padding: 10px 16px;
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .5px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  white-space: nowrap;
}
tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}
tbody tr { transition: background var(--transition); }
tbody tr:hover { background: #f9fafb; }
tbody tr:last-child td { border-bottom: none; }
td.mono {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-secondary);
}
.clickable-row { cursor: pointer; }

/* ── Badges ── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .2px;
  white-space: nowrap;
}
.badge-created   { background: var(--info-light);    color: var(--info); }
.badge-routing   { background: var(--warning-light);  color: var(--warning); }
.badge-routed    { background: var(--primary-light);  color: var(--primary); }
.badge-order_recreating { background: var(--warning-light); color: var(--warning); }
.badge-order_recreated  { background: var(--success-light); color: var(--success); }
.badge-shipped   { background: var(--success-light);  color: #059669; }
.badge-completed { background: #f0fdf4;               color: #15803d; }
.badge-error     { background: var(--error-light);     color: var(--error); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
  font-family: var(--font);
  line-height: 1.4;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #eef0f4; }
.btn-danger { background: var(--error); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #059669; }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Forms ── */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: .3px;
}
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,110,247,.12);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-inline {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.form-inline .form-group { margin-bottom: 0; flex: 1; }

/* ── Filters Bar ── */
.filters-bar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.filters-bar .form-input,
.filters-bar .form-select {
  width: auto;
  min-width: 160px;
}
.search-input {
  position: relative;
}
.search-input input {
  padding-left: 34px;
}
.search-input svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
}

/* ── Detail Layout ── */
.detail-grid {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 20px;
  align-items: start;
}
@media (max-width: 1000px) {
  .detail-grid { grid-template-columns: 1fr; }
}

.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.detail-meta {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  margin-top: 4px;
}
.detail-meta-item {
  font-size: 13px;
  color: var(--text-secondary);
}
.detail-meta-item strong {
  color: var(--text);
  font-weight: 600;
}

/* ── Attributes Key-Value ── */
.kv-list { list-style: none; }
.kv-item {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-light);
}
.kv-item:last-child { border-bottom: none; }
.kv-key {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 120px;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: .3px;
}
.kv-value {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  word-break: break-all;
}
.kv-empty {
  color: var(--text-muted);
  font-style: italic;
  padding: 12px 0;
  font-size: 13px;
}

/* ── Timeline / Events ── */
.timeline { list-style: none; }
.timeline-item {
  display: flex;
  gap: 14px;
  padding: 10px 0;
  position: relative;
}
.timeline-item:not(:last-child)::before {
  content: '';
  position: absolute;
  left: 7px;
  top: 30px;
  bottom: -2px;
  width: 2px;
  background: var(--border);
}
.timeline-dot {
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  margin-top: 2px;
  z-index: 1;
}
.timeline-item.event-created .timeline-dot  { border-color: var(--info); background: var(--info-light); }
.timeline-item.event-updated .timeline-dot  { border-color: var(--primary); background: var(--primary-light); }
.timeline-item.event-attachment_added .timeline-dot { border-color: var(--success); background: var(--success-light); }
.timeline-content { flex: 1; min-width: 0; }
.timeline-type {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .3px;
  color: var(--text-secondary);
}
.timeline-detail {
  font-size: 13px;
  color: var(--text);
  margin-top: 2px;
  word-break: break-word;
}
.timeline-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 3px;
}

/* ── Attachments List ── */
.attachment-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-light);
}
.attachment-item:last-child { border-bottom: none; }
.attachment-icon {
  width: 36px; height: 36px;
  border-radius: 8px;
  background: var(--primary-light);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.attachment-info { flex: 1; min-width: 0; }
.attachment-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
}

/* ── Upload Area ── */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  cursor: pointer;
  transition: all var(--transition);
  background: var(--bg);
}
.upload-area:hover { border-color: var(--primary); background: var(--primary-light); }
.upload-area.dragover { border-color: var(--primary); background: var(--primary-light); }
.upload-hint {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 6px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }
.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 520px;
  max-width: 92vw;
  max-height: 85vh;
  overflow-y: auto;
  transform: translateY(10px);
  transition: transform 200ms;
}
.modal-overlay.open .modal { transform: translateY(0); }
.modal-header {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-title { font-size: 16px; font-weight: 700; }
.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 20px; }
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-light);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ── Toast ── */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 12px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toast-in 300ms ease;
  max-width: 360px;
}
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--error); color: #fff; }
.toast-info { background: var(--info); color: #fff; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--border-light);
  font-size: 13px;
  color: var(--text-secondary);
}
.pagination-buttons {
  display: flex;
  gap: 4px;
}

/* ── Back Link ── */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--text-secondary);
  text-decoration: none;
  margin-bottom: 16px;
  font-weight: 500;
}
.back-link:hover { color: var(--primary); }

/* ── Empty State ── */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}
.empty-state-icon {
  font-size: 40px;
  margin-bottom: 12px;
  opacity: .5;
}
.empty-state-text {
  font-size: 14px;
}

/* ── Loading ── */
.spinner {
  display: inline-block;
  width: 20px; height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-center {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

/* ── Login ── */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 300ms;
}
.login-overlay.hidden { opacity: 0; pointer-events: none; }
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 40px 36px;
  width: 380px;
  max-width: 92vw;
}
.login-brand {
  text-align: center;
  margin-bottom: 28px;
}
.app-hidden { visibility: hidden; }

/* ── Responsive ── */
@media (max-width: 768px) {
  .sidebar { width: 60px; }
  .sidebar .brand-name, .sidebar .brand-sub, .sidebar .nav-item span,
  .sidebar .status-text { display: none; }
  .sidebar-brand { justify-content: center; padding: 16px 0; }
  .nav-item { justify-content: center; padding: 12px; }
  .main { margin-left: 60px; padding: 20px 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
