:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e6e8eb;
  --text: #1a1d21;
  --muted: #6b7280;
  --accent: #0f9d58;
  --accent-dark: #0b7a45;
  --accent-soft: #e7f6ee;
  --green: #0f9d58;
  --green-bg: #e7f6ee;
  --red: #e5484d;
  --red-bg: #fdecec;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, 0.18);
  --radius: 12px;
  --radius-sm: 8px;
}

:root[data-theme="dark"] {
  --bg: #0e1116;
  --surface: #171b21;
  --surface-2: #1d222a;
  --border: #2a2f38;
  --text: #e7eaee;
  --muted: #9aa4b2;
  --accent: #2ea36a;
  --accent-dark: #35b877;
  --accent-soft: #172c22;
  --green: #3fb981;
  --green-bg: #14291f;
  --red: #f0676b;
  --red-bg: #2c1618;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
.block { width: 100%; }

/* ---- Login ---- */
.login-overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), var(--bg));
  padding: 20px;
}
.login-card {
  background: var(--surface); padding: 32px; border-radius: 16px;
  box-shadow: var(--shadow-lg); width: 340px; max-width: 100%;
  display: flex; flex-direction: column; gap: 14px; border: 1px solid var(--border);
}
.login-brand { font-size: 24px; font-weight: 700; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 500; }
.login-card input {
  padding: 11px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; background: var(--surface-2); color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.login-card #login-code { font-size: 20px; letter-spacing: 4px; text-align: center; }
.login-card label.check { flex-direction: row; align-items: center; gap: 8px; cursor: pointer; }
.login-card label.check input { width: 16px; height: 16px; padding: 0; margin: 0; accent-color: var(--accent); }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 18px;
  padding: 0 18px; height: 58px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5; box-shadow: var(--shadow-sm);
}
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  background: transparent; color: var(--muted); border: 0; cursor: pointer;
  padding: 8px 14px; border-radius: 999px; font-size: 14px; font-weight: 500; white-space: nowrap;
  font-family: inherit;
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--accent); color: #fff; font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.who { color: var(--muted); font-weight: 500; font-size: 13px; }
.icon-btn {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  width: 34px; height: 34px; cursor: pointer; font-size: 15px; color: var(--text);
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--border); }

/* ---- Toolbar ---- */
.toolbar { display: flex; align-items: center; gap: 10px; padding: 14px 18px; flex-wrap: wrap; }
.search-box { position: relative; display: flex; align-items: center; }
.search-ico { position: absolute; left: 11px; opacity: .5; font-size: 13px; pointer-events: none; }
.toolbar input[type=search] {
  padding: 9px 12px 9px 32px; border: 1px solid var(--border); border-radius: 999px;
  font-size: 14px; min-width: 240px; background: var(--surface); color: var(--text); font-family: inherit;
}
.toolbar select {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 999px;
  font-size: 14px; background: var(--surface); color: var(--text); font-family: inherit; cursor: pointer;
}
.toolbar input:focus, .toolbar select:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.count { color: var(--muted); font-size: 13px; font-weight: 500; }

/* ---- Buttons ---- */
.btn {
  padding: 9px 16px; border-radius: 999px; border: 1px solid transparent; cursor: pointer;
  font-size: 14px; font-weight: 600; font-family: inherit; transition: background .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn.sm { padding: 7px 13px; font-size: 13px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.ghost:disabled { opacity: .45; cursor: not-allowed; }
.btn.danger { background: var(--red); color: #fff; }
.row-btn {
  padding: 5px 9px; font-size: 14px; background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer; color: var(--text);
}
.row-btn:hover { background: var(--surface-2); }

/* ---- Table card ---- */
.table-wrap { padding: 0 18px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); overflow: auto; max-height: calc(100vh - 58px - 66px - 60px);
}
table { border-collapse: collapse; width: 100%; }
thead th {
  position: sticky; top: 0; background: var(--surface-2); z-index: 2;
  text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border);
  font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  font-weight: 600; white-space: nowrap; cursor: pointer; user-select: none;
}
thead th.nosort { cursor: default; }
thead th:not(.nosort):hover { color: var(--text); }
thead th .arrow { color: var(--accent); }
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; max-width: 340px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }

.cell { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.cell-val { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-val.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 13px; }
.copy-ico { opacity: 0; font-size: 13px; color: var(--muted); flex-shrink: 0; transition: opacity .12s; }
td.copyable { cursor: pointer; }
td.copyable:hover .copy-ico { opacity: .6; }

/* ---- Status badge ---- */
.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 11px; border-radius: 999px;
  font-size: 12px; font-weight: 600; cursor: pointer; border: 1px solid transparent; font-family: inherit;
}
.badge .dot { width: 7px; height: 7px; border-radius: 50%; }
.badge.GREEN { background: var(--green-bg); color: var(--green); }
.badge.GREEN .dot { background: var(--green); }
.badge.RED { background: var(--red-bg); color: var(--red); }
.badge.RED .dot { background: var(--red); }
.badge:hover { filter: brightness(0.97); }
.actions-cell { white-space: nowrap; display: flex; gap: 6px; }

/* ---- Global search ---- */
.global { display: flex; flex-direction: column; gap: 16px; }
.global-group {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); overflow: hidden;
}
.global-group-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  background: var(--surface-2); border-bottom: 1px solid var(--border); cursor: pointer;
}
.global-group-head .g-label { font-weight: 700; font-size: 15px; }
.global-group-head .g-count {
  font-size: 12px; font-weight: 600; color: var(--accent);
  background: var(--accent-soft); padding: 2px 9px; border-radius: 999px;
}
.global-group-head .g-open { margin-left: auto; color: var(--muted); font-size: 13px; }
.global-group table { width: 100%; }
.global-empty { padding: 60px 20px; text-align: center; color: var(--muted); }
.global-empty .empty-emoji { font-size: 40px; margin-bottom: 8px; }

/* ---- Pager ---- */
.pager { display: flex; align-items: center; gap: 14px; padding: 14px 18px; }

/* ---- Empty ---- */
.empty { padding: 60px 20px; text-align: center; color: var(--muted); }
.empty-emoji { font-size: 40px; margin-bottom: 8px; }
.empty p { margin: 0 0 14px; }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 20, 28, .5); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; z-index: 20; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: 16px; padding: 24px; width: 460px; max-width: 100%;
  max-height: 90vh; overflow: auto; box-shadow: var(--shadow-lg); border: 1px solid var(--border);
}
.modal.small { width: 380px; }
.modal h2 { margin: 0 0 16px; font-size: 19px; }
.record-form { display: flex; flex-direction: column; gap: 13px; }
.record-form label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.record-form input, .record-form textarea, .record-form select {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; font-family: inherit; background: var(--surface-2); color: var(--text);
}
.record-form input:focus, .record-form textarea:focus, .record-form select:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.record-form textarea { min-height: 64px; resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.error { color: var(--red); font-size: 13px; min-height: 16px; margin: 8px 0 0; }

/* ---- Google Drive sync pill ---- */
.drive-status {
  display: inline-flex; align-items: center; gap: 6px; max-width: 280px; min-width: 0;
  white-space: nowrap;
  padding: 6px 11px; border-radius: 999px; font-size: 12px; font-weight: 600;
  text-decoration: none; cursor: default; transition: background .15s, color .15s;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
}
/* the text lives in a child span: text-overflow is ignored on flex containers */
.drive-status-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drive-status[href], .drive-status[data-state="error"], .drive-status[data-state="pending"] { cursor: pointer; }
.drive-status:focus-visible { outline: 2px solid var(--accent, #2563eb); outline-offset: 2px; }
/* light-theme text colours are darkened to reach 4.5:1 on the tinted pill */
.drive-status.ok { color: #0b7a45; border-color: rgba(15, 157, 88, .35); background: rgba(15, 157, 88, .10); }
.drive-status.warn { color: #8a5a00; border-color: rgba(214, 158, 46, .45); background: rgba(214, 158, 46, .14); }
.drive-status.err { color: #c0262c; border-color: rgba(229, 72, 77, .35); background: rgba(229, 72, 77, .10); }
:root[data-theme="dark"] .drive-status.ok { color: var(--green); border-color: rgba(63, 185, 129, .4); background: rgba(63, 185, 129, .14); }
:root[data-theme="dark"] .drive-status.warn { color: #f2c46d; }
:root[data-theme="dark"] .drive-status.err { color: var(--red); border-color: rgba(240, 103, 107, .4); background: rgba(240, 103, 107, .14); }
.drive-status.warn::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }

/* ---- Toast ---- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1a1d21; color: #fff; padding: 11px 20px; border-radius: 999px;
  box-shadow: var(--shadow-lg); z-index: 40; font-weight: 500; font-size: 13px;
  animation: rise .2s ease;
}
:root[data-theme="dark"] .toast { background: #e7eaee; color: #1a1d21; }
.toast.err { background: var(--red); color: #fff; }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

@media (max-width: 720px) {
  .brand span { display: none; }
  /* let the header wrap: controls stay reachable and the section tabs get their own full row */
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; gap: 8px 12px; }
  .tabs { order: 3; flex: 1 0 100%; }
  .topbar-right { margin-left: auto; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
  .who { display: none; }
  .drive-status { max-width: 170px; }
  #download-copy span, #recovery-key span { display: none; }
  .toolbar input[type=search] { min-width: 140px; }
  .card { max-height: none; }
}
