:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #eff4ff;
  --ok: #16a34a;
  --ok-soft: #f0fdf4;
  --err: #dc2626;
  --err-soft: #fef2f2;
  --muted-soft: #f3f4f6;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.layout { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: #111827;
  color: #e5e7eb;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { padding: 20px 18px 16px; border-bottom: 1px solid #1f2937; }
.brand-title { font-size: 18px; font-weight: 700; color: #ffffff; }
.brand-sub { font-size: 12px; color: #9ca3af; margin-top: 2px; }

.nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }

.nav-link {
  display: block;
  padding: 9px 12px;
  border-radius: 8px;
  color: #d1d5db;
  text-decoration: none;
  font-weight: 500;
}

.nav-link:hover { background: #1f2937; color: #ffffff; }
.nav-link.active { background: var(--accent); color: #ffffff; }

.sidebar-footer { padding: 14px 18px; border-top: 1px solid #1f2937; font-size: 12px; color: #9ca3af; }
.status-line { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.status-line .label { color: #9ca3af; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: 0 0 8px; }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.dot.pending { background: #9ca3af; }

/* ---------- Main ---------- */
.main { flex: 1; min-width: 0; }

.topbar {
  padding: 20px 28px 0;
  display: flex;
  align-items: baseline;
  gap: 16px;
}

.topbar h1 { margin: 0; font-size: 22px; font-weight: 700; }
.page-subtitle { color: var(--text-muted); font-size: 13px; }

.view { padding: 20px 28px 40px; }

/* ---------- Panels / cards ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 20px;
}

.panel h2 { margin: 0 0 14px; font-size: 15px; font-weight: 600; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.card .label { color: var(--text-muted); font-size: 12px; }
.card .value { font-size: 26px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.card .sub { color: var(--text-muted); font-size: 12px; margin-top: 4px; }

/* ---------- Toolbar / filters ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.toolbar .spacer { flex: 1; }

.input, .select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  min-width: 160px;
}

.button {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.button:hover { background: var(--muted-soft); }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.button.primary:hover { background: #1d4ed8; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--panel); }
.seg button {
  border: none;
  background: transparent;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text-muted);
}
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.seg button:not(:last-child) { border-right: 1px solid var(--border); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th {
  text-align: left;
  padding: 9px 12px;
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: #fafafa;
}
table.data td {
  padding: 9px 12px;
  border-bottom: 1px solid #f3f4f6;
  vertical-align: top;
}
table.data tbody tr:hover { background: #fafafa; }
table.data tbody tr.clickable { cursor: pointer; }
table.data .mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Badges / states ---------- */
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.muted { background: var(--muted-soft); color: var(--text-muted); }

.state { padding: 40px 16px; text-align: center; color: var(--text-muted); }
.state.error { color: var(--err); }

/* ---------- Detail pages ---------- */
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.detail-item { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; }
.detail-item .label { color: var(--text-muted); font-size: 12px; }
.detail-item .value { margin-top: 4px; font-size: 14px; font-weight: 600; word-break: break-all; }
.detail-item .value.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; font-weight: 400; }

/* ---------- Activity list ---------- */
.activity { display: flex; flex-direction: column; }
.activity-item {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 9px 0;
  border-bottom: 1px solid #f3f4f6;
}
.activity-item:last-child { border-bottom: none; }
.activity-item .name { font-weight: 600; min-width: 150px; }
.activity-item .meta { color: var(--text-muted); font-size: 12px; flex: 1; }
.activity-item .time { color: var(--text-muted); font-size: 12px; white-space: nowrap; }

/* ---------- System page ---------- */
.kv { display: grid; grid-template-columns: 200px 1fr; gap: 6px 16px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; font-weight: 500; word-break: break-all; }


/* ---------- AI Control Plane V2.1 ---------- */
.ai-status-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 16px;
  background: var(--accent-soft);
  border: 1px solid #dbeafe;
  border-radius: 10px;
  margin-bottom: 16px;
  font-size: 15px;
}
.ai-status-label {
  color: var(--text-muted);
  font-weight: 600;
}
.ai-status-value {
  font-weight: 800;
  color: var(--accent);
}

.ai-provider-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.ai-provider-select-label {
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}
.ai-provider-dropdown {
  position: relative;
  min-width: 240px;
  max-width: 420px;
  flex: 1 1 auto;
}
.ai-provider-dropdown-toggle {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}
.ai-provider-dropdown-toggle::-webkit-details-marker {
  display: none;
}
.ai-provider-dropdown-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.ai-provider-dropdown-menu {
  position: absolute;
  z-index: 50;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  max-height: min(320px, 60vh);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  padding: 4px;
}
.ai-provider-dropdown-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border: none;
  background: transparent;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  min-height: 44px;
}
.ai-provider-dropdown-option:hover,
.ai-provider-dropdown-option.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.ai-provider-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
  font-size: 15px;
}

.ai-config-grid,
.ai-global-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  max-width: 1200px;
}
.ai-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  min-width: 0;
}
.ai-field .input,
.ai-field .select {
  width: 100%;
  min-width: 0;
  min-height: 42px;
}
.ai-field-label {
  font-size: 13px;
  color: var(--text-muted);
}
.ai-api-key-field {
  grid-column: span 2;
}
.ai-api-key-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.ai-api-key-row .input {
  flex: 1 1 220px;
  min-width: 0;
}
.ai-api-key-row .button {
  min-height: 42px;
  white-space: nowrap;
}

.ai-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 16px;
}
.ai-actions .button {
  min-height: 44px;
  padding: 8px 18px;
}
.ai-actions .page-subtitle {
  flex: 1 1 200px;
  margin: 0;
}

/* Desktop: 3-column provider/global field grid */
@media (min-width: 1200px) {
  .ai-config-grid,
  .ai-global-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* Laptop / small desktop: 2 columns */
@media (min-width: 768px) and (max-width: 1199px) {
  .ai-config-grid,
  .ai-global-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .ai-api-key-field {
    grid-column: span 2;
  }
}

/* Tablet / mobile: 1 column, no horizontal overflow */
@media (max-width: 900px) {
  .ai-config-grid,
  .ai-global-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  .ai-api-key-field {
    grid-column: span 1;
  }
  .ai-provider-dropdown {
    width: 100%;
    max-width: none;
  }
  .ai-status-banner,
  .ai-provider-bar,
  .ai-actions {
    align-items: stretch;
  }
  .ai-actions .button {
    width: 100%;
    justify-content: center;
  }
  .ai-api-key-row {
    align-items: stretch;
    flex-direction: column;
  }
  .ai-api-key-row .button {
    width: 100%;
  }
}

/* Global admin responsive adaptation (same app, not a mobile site) */
@media (max-width: 767px) {
  .layout {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    flex: none;
    height: auto;
    position: static;
    padding-bottom: 8px;
  }
  .brand {
    padding: 12px 16px 8px;
  }
  .nav {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 4px;
  }
  .nav-link {
    padding: 8px 10px;
  }
  .sidebar-footer {
    padding: 8px 16px;
  }
  .main {
    width: 100%;
  }
  .topbar {
    padding: 16px 16px 0;
  }
  .view {
    padding: 16px 16px 32px;
  }
  .panel {
    padding: 14px 14px;
  }
  .toolbar {
    align-items: stretch;
  }
  .toolbar .input,
  .toolbar .select,
  .toolbar .button {
    width: 100%;
    min-width: 0;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .sidebar {
    width: 190px;
    flex-basis: 190px;
  }
  .topbar {
    padding-left: 20px;
    padding-right: 20px;
  }
  .view {
    padding-left: 20px;
    padding-right: 20px;
  }
}
