/* ===========================================================================
   Themes — switch by setting <html data-theme="light|dark|ocean">
   Default (":root") is the light theme.
   =========================================================================== */
:root,
:root[data-theme='light'] {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --on-primary: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --sidebar: #ffffff;
  --code-bg: #f8fafc;
  --shadow: rgba(15, 23, 42, 0.12);
}

:root[data-theme='dark'] {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273549;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --on-primary: #ffffff;
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --sidebar: #131c31;
  --code-bg: #0b1220;
  --shadow: rgba(0, 0, 0, 0.4);
}

:root[data-theme='ocean'] {
  --bg: #06202a;
  --panel: #0b2b38;
  --panel-2: #0f3a4a;
  --border: #164e5f;
  --text: #d7f2f5;
  --muted: #7fb4bd;
  --primary: #14b8a6;
  --primary-hover: #0d9488;
  --on-primary: #04252b;
  --danger: #f87171;
  --ok: #34d399;
  --warn: #fbbf24;
  --sidebar: #082530;
  --code-bg: #04191f;
  --shadow: rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }
.error { color: var(--danger); }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; margin-left: 10px; }

/* ===========================================================================
   Login
   =========================================================================== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 36px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 20px 60px var(--shadow);
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 12px; color: var(--muted); }

/* ===========================================================================
   Inputs / buttons
   =========================================================================== */
input, select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 14px;
  outline: none;
}
input:focus, select:focus { border-color: var(--primary); }

button {
  cursor: pointer;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
}
button:hover { filter: brightness(1.08); }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
button.primary:hover { background: var(--primary-hover); }
button.ghost { background: transparent; }
button.small { padding: 5px 10px; font-size: 13px; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); }

/* ===========================================================================
   Layout: sidebar + content
   =========================================================================== */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.side-brand { display: flex; align-items: center; gap: 12px; padding: 6px 8px 18px; }
.logo {
  width: 42px; height: 42px; border-radius: 11px;
  background: var(--primary); color: var(--on-primary);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 14px; letter-spacing: 0.5px;
}
.side-title { font-weight: 700; line-height: 1.35; font-size: 14px; }

.side-nav { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  text-align: left; width: 100%;
  background: transparent; border: none; color: var(--muted);
  padding: 10px 12px; border-radius: 9px; font-size: 14px;
}
.nav-item:hover { background: var(--panel-2); color: var(--text); filter: none; }
.nav-item.active { background: var(--primary); color: var(--on-primary); font-weight: 600; }
.nav-ico { font-size: 16px; }

.side-user { display: flex; align-items: center; gap: 8px; }
.side-user .user-ico { font-size: 14px; }
.side-user #side-username { font-weight: 600; }

.content { flex: 1; min-width: 0; padding: 20px 30px 30px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Top bar: theme switch + user info, right-aligned, full-width adaptive */
.topbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 16px;
  padding-bottom: 14px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
}
.topbar-right { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; justify-content: flex-end; }
.topbar-divider { width: 1px; height: 22px; background: var(--border); }

/* Theme dropdown: single button + click-to-open menu */
.theme-dropdown { position: relative; }
.theme-trigger { display: inline-flex; align-items: center; gap: 8px; padding: 7px 12px; font-size: 13px; }
.theme-trigger .tri-ico { font-size: 14px; line-height: 1; }
.theme-trigger .tri-label { color: var(--text); }
.theme-trigger #theme-current-label { font-weight: 600; }
.theme-trigger .caret { font-size: 10px; color: var(--muted); transition: transform 0.15s ease; }
.theme-dropdown.open .theme-trigger .caret { transform: rotate(180deg); }
.theme-dropdown.open .theme-trigger { border-color: var(--primary); }

.theme-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  min-width: 148px; padding: 6px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px var(--shadow);
  display: flex; flex-direction: column; gap: 2px; z-index: 60;
}
.theme-option {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  background: transparent; border: none; color: var(--text);
  padding: 8px 10px; border-radius: 8px; font-size: 13px; font-weight: 500;
}
.theme-option:hover { background: var(--panel-2); filter: none; }
.theme-option.active { color: var(--primary); font-weight: 700; background: color-mix(in srgb, var(--primary) 12%, transparent); }
.theme-option.active::after { content: '✓'; margin-left: auto; font-weight: 700; }
.theme-option .dot { width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--border); flex-shrink: 0; }
.theme-option .dot.light { background: #2563eb; }
.theme-option .dot.dark { background: #1e293b; }
.theme-option .dot.ocean { background: #14b8a6; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 8px; }
.page-head h2 { margin: 0; font-size: 20px; }

h3 { margin: 26px 0 10px; font-size: 15px; }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.toolbar select { min-width: 200px; }

/* ===========================================================================
   Cards & tables
   =========================================================================== */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; box-shadow: 0 4px 16px var(--shadow); }
.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.grid th { background: var(--panel-2); font-weight: 600; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; }
.grid tr:last-child td { border-bottom: none; }
.grid td input, .grid td select { width: 100%; }
.drag-col { width: 30px; }
.drag-handle { cursor: grab; color: var(--muted); user-select: none; }
.mono { font-family: 'SF Mono', ui-monospace, Menlo, monospace; }
.sortable-ghost { opacity: 0.4; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.badge.key { background: var(--panel-2); border: 1px solid var(--border); }

.add-box { border: 1px dashed var(--border); border-radius: 12px; margin-top: 16px; padding: 12px 16px; }
.add-box legend { color: var(--muted); padding: 0 6px; }
.row-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row-form input { min-width: 130px; flex: 1; }
.chk { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 999px; }

/* ===========================================================================
   Modal / toast
   =========================================================================== */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; width: min(720px, 92vw); max-height: 86vh; display: flex; flex-direction: column; padding: 18px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h3 { margin: 0; }
.code { background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: 14px; overflow: auto; font-family: 'SF Mono', ui-monospace, Menlo, monospace; font-size: 13px; line-height: 1.5; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; flex-wrap: wrap; }

.issue { padding: 6px 10px; border-radius: 6px; margin-bottom: 6px; font-size: 13px; }
.issue.error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.issue.warning { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }
.issue.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }

.snap-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--border); padding: 10px 18px; border-radius: 10px; z-index: 100; box-shadow: 0 10px 30px var(--shadow); }
.toast.ok { border-color: var(--ok); }
.toast.err { border-color: var(--danger); }

.iconbtn { padding: 4px 8px; font-size: 13px; }

/* ===========================================================================
   Banner / account settings / MFA
   =========================================================================== */
.banner {
  background: color-mix(in srgb, var(--warn) 16%, transparent);
  color: var(--warn);
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
  padding: 10px 14px; border-radius: 10px; margin-bottom: 16px;
  display: flex; align-items: center; gap: 10px;
}
.link { background: transparent; border: none; color: var(--primary); text-decoration: underline; padding: 0; }
.link:hover { filter: none; }

.notice {
  background: color-mix(in srgb, var(--warn) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
  color: var(--text);
  padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; line-height: 1.7;
}
.notice code { background: var(--panel-2); border: 1px solid var(--border); padding: 1px 6px; border-radius: 6px; }

.card.pad { padding: 18px; }
.stack { display: flex; flex-direction: column; gap: 12px; max-width: 420px; }

.mfa-status { margin-bottom: 14px; }
.mfa-actions { display: flex; gap: 10px; margin-top: 16px; }
.mfa-grid { display: flex; gap: 24px; flex-wrap: wrap; align-items: flex-start; margin-top: 8px; }
.qr-box { background: #fff; padding: 12px; border-radius: 10px; border: 1px solid var(--border); width: 188px; height: 188px; display: flex; align-items: center; justify-content: center; }
.mfa-secret { min-width: 240px; }
.mfa-secret code { display: inline-block; background: var(--panel-2); border: 1px solid var(--border); padding: 6px 10px; border-radius: 8px; letter-spacing: 1px; word-break: break-all; }

/* ===========================================================================
   Responsive
   =========================================================================== */
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: relative; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px; }
  .side-nav { flex-direction: row; flex-wrap: wrap; margin-top: 0; }
  .content { padding: 16px 18px 24px; }
  .topbar { flex-wrap: wrap; justify-content: flex-start; }
  .topbar-right { width: 100%; justify-content: flex-start; }
}
