:root {
  --bg: #0a0f1a;
  --panel: #0e1524;
  --surface: #121a2b;
  --surface-2: #172135;
  --input: #0c1322;
  --border: #1f2a3e;
  --border-strong: #2d3a52;
  --text: #f3f6fa;
  --text-dim: #b4bfcc;
  --text-faint: #8592a3;
  --accent: #f5893a;
  --accent-2: #e8702a;
  --accent-soft: #f5893a1f;
  --ring: #f5893a55;
  --blue: #3b6cf6;
  --blue-hover: #4d7bff;
  --good: #4ade80;
  --good-soft: #22c55e1f;
  --bad: #f87171;
  --bad-soft: #ef44441a;
  --shadow: 0 1px 2px #0008, 0 10px 30px #0004;
}
:root {
  --brand: #f5893a;
  --brand-2: #e8702a;
  --on-brand: #1c0d02;
  --bg-image: none;
  --header-bg: transparent;
  --tile-blue: var(--surface);
  --tile-purple: var(--surface);
  --tile-green: var(--surface);
  --tile-red: var(--surface);
  --icon-blue: #7aa2ff;
  --icon-purple: #b197fc;
}
:root[data-theme="light"] {
  --bg: #e8ebf7;
  --bg-image: radial-gradient(900px 500px at 0% 0%, #dfe4ff 0%, transparent 60%),
              radial-gradient(900px 500px at 100% 100%, #ece3ff 0%, transparent 60%);
  --panel: #f7f8fd;
  --header-bg: linear-gradient(100deg, #eef0ff 0%, #f5f3ff 60%, #ece9fb 100%);
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --input: #f3f5fa;
  --border: #e1e5ef;
  --border-strong: #ccd2e0;
  --text: #111827;
  --text-dim: #3f4859;
  --text-faint: #636d7e;
  --accent: #5b5ef0;
  --accent-2: #4a4de0;
  --accent-soft: #5b5ef01a;
  --ring: #5b5ef040;
  --brand: #6366f1;
  --brand-2: #4f46e5;
  --on-brand: #ffffff;
  --blue: #3b6cf6;
  --blue-hover: #2f5fe0;
  --good: #15803d;
  --good-soft: #22c55e1f;
  --bad: #b91c1c;
  --bad-soft: #ef44441a;
  --tile-blue: #e6ecfd;
  --tile-purple: #ece8fc;
  --tile-green: #e5f5ea;
  --tile-red: #fbe9ec;
  --icon-blue: #3b6cf6;
  --icon-purple: #7c3aed;
  --shadow: 0 1px 2px #1e1b4b0a, 0 10px 30px #1e1b4b0f;
}

* { box-sizing: border-box; }
html { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg-image), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
.mono { font-family: 'JetBrains Mono', ui-monospace, monospace; }
::selection { background: var(--ring); }
svg { flex-shrink: 0; }

input, select {
  font-family: inherit;
  font-size: 13.5px;
  color: var(--text);
  background: var(--input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: var(--text-faint); }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
input[readonly] { cursor: default; }
input[readonly]:focus { box-shadow: none; border-color: var(--border); }

button {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 600;
  padding: 10px 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
  color: var(--text);
  background: none;
  transition: background .15s, border-color .15s, color .15s, opacity .15s, transform .05s;
}
button:active { transform: translateY(1px); }
button:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
button:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary {
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: var(--on-brand);
  box-shadow: 0 6px 18px var(--ring);
}
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { border-color: var(--border-strong); color: var(--text-dim); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }

/* ---- Gate ---- */
#gate { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px 26px;
  box-shadow: var(--shadow);
  text-align: center;
}
.gate-card h2 { font-size: 18px; margin: 16px 0 6px; }
.gate-card p { color: var(--text-dim); font-size: 13px; margin: 0 0 22px; line-height: 1.5; }
.gate-card input { margin-bottom: 12px; padding: 11px 13px; font-size: 14px; }
.gate-card button { width: 100%; }
.gate-card .err { color: var(--bad); font-size: 12.5px; min-height: 18px; margin-top: 10px; line-height: 1.4; }

.logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: inline-grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
}
:root[data-theme="light"] .logo { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; }
.logo svg { width: 20px; height: 20px; }

/* ---- Shell ---- */
.shell {
  max-width: 1360px;
  margin: 24px auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 22px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand h1 { font-size: 19px; font-weight: 700; margin: 0; letter-spacing: -0.01em; }
.brand .sub { color: var(--text-dim); font-size: 13px; margin-top: 3px; }
.header-pill {
  display: flex;
  align-items: center;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface);
  overflow: hidden;
}
.header-pill button { border-radius: 0; padding: 10px 16px; color: var(--text-dim); }
.header-pill button:hover { color: var(--text); background: var(--surface-2); }
.header-pill .divider { width: 1px; align-self: stretch; background: var(--border); }
.header-pill svg { width: 17px; height: 17px; }
#lock-btn.locked { color: var(--text-dim); }
#lock-btn.unlocked { color: var(--good); background: var(--good-soft); }

main { padding: 22px 28px 28px; }

/* ---- Stats ---- */
.stats-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)) auto;
  gap: 12px;
  align-items: center;
  margin-bottom: 20px;
}
.stat {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  min-width: 0;
}
.stat-icon {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--surface-2);
  color: var(--icon-blue);
}
.stat.blue { background: var(--tile-blue); }
.stat.purple { background: var(--tile-purple); }
.stat.purple .stat-icon { color: var(--icon-purple); }
.stat.green { background: var(--tile-green); }
.stat.red { background: var(--tile-red); }
:root[data-theme="light"] .stat { border-color: transparent; }
:root[data-theme="light"] .stat-icon { background: #ffffffb3; }
.stat-icon svg { width: 20px; height: 20px; }
.stat-icon .dot { width: 12px; height: 12px; border-radius: 50%; }
.stat .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat .k { color: var(--text-dim); font-size: 12.5px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#add-group-btn { height: 54px; padding: 0 30px; font-size: 15px; }
#add-group-btn svg { width: 18px; height: 18px; }

/* ---- Toolbar ---- */
.toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.search { position: relative; flex: 1; max-width: 700px; }
.search svg {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 17px;
  height: 17px;
  color: var(--text-faint);
  pointer-events: none;
}
.search input { padding: 11px 14px 11px 42px; background: var(--surface); }
.toolbar .spacer { flex: 1; }
.select { position: relative; }
.select select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface);
  padding: 11px 40px 11px 14px;
  min-width: 170px;
  cursor: pointer;
  border-color: var(--border-strong);
}
.select.with-icon select { padding-left: 40px; min-width: 210px; }
.select > svg {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-dim);
  pointer-events: none;
}
.select .lead { left: 14px; }
.select .chev { right: 14px; }
.view-toggle {
  display: flex;
  padding: 4px;
  gap: 4px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--surface);
}
.view-toggle button { padding: 7px 12px; border-radius: 7px; color: var(--text-dim); }
.view-toggle button.active { background: var(--accent-soft); color: var(--accent); }
.view-toggle svg { width: 17px; height: 17px; }

/* ---- Add form ---- */
.add-card {
  background: var(--surface);
  border: 1px dashed var(--ring);
  border-radius: 12px;
  padding: 18px;
  margin-bottom: 16px;
}
.add-card h3 { margin: 0 0 4px; font-size: 15px; }
.add-card .hint { margin: 0 0 14px; font-size: 13px; color: var(--text-dim); }
code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--border);
}
.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.add-actions { display: flex; gap: 8px; }

/* ---- Rows ---- */
#cards { display: grid; gap: 10px; }
#cards.grid { grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 14px; }

.row {
  --c: #6b8cff;
  position: relative;
  display: grid;
  grid-template-columns: minmax(240px, 1.3fr) 130px minmax(0, 2.4fr) auto;
  align-items: center;
  gap: 20px;
  padding: 14px 18px 14px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  transition: border-color .2s;
}
.row::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--c);
}
.row.dirty { border-color: var(--ring); }
.row.editing { border-color: var(--border-strong); }

.ident { display: flex; align-items: center; gap: 16px; min-width: 0; }
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--c);
  background: color-mix(in srgb, var(--c) 18%, transparent);
}
.avatar svg { width: 22px; height: 22px; }
.ident-text { min-width: 0; flex: 1; }
.name-input {
  background: transparent;
  border: 1px solid transparent;
  font-size: 16px;
  font-weight: 700;
  padding: 2px 6px;
  margin-left: -7px;
  border-radius: 6px;
  text-overflow: ellipsis;
}
.row.editing .name-input { background: var(--input); border-color: var(--border); }
.id-input {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-size: 13px;
  padding: 1px 5px;
  margin-left: -6px;
  border-radius: 5px;
  width: 16ch;
  min-width: 0;
}
.row.editing .id-input { background: var(--input); border-color: var(--border); color: var(--text); }
.id-line { display: flex; align-items: center; gap: 8px; margin-top: 5px; color: var(--text-dim); font-size: 13px; }
.id-tag {
  font-size: 11.5px;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 1px 7px;
  border-radius: 5px;
}
.copy-btn { padding: 2px; border-radius: 5px; color: var(--text-faint); }
.copy-btn:hover { color: var(--text); }
.copy-btn svg { width: 15px; height: 15px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--good-soft);
  color: var(--good);
  white-space: nowrap;
}
.badge::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.multi { background: var(--accent-soft); color: var(--accent); }
.badge.none { background: var(--bad-soft); color: var(--bad); }

.plans-head, .plan-row {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
  align-items: center;
}
.row.editing .plans-head, .row.editing .plan-row { grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr) 30px; }
.plans-head .cmd { font-family: 'JetBrains Mono', monospace; font-size: 11px; color: var(--text-faint); }
@media (max-width: 560px) {
  .plans-head { display: none; }
  .plan-row, .row.editing .plan-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 8px; }
  .plan-row .field { grid-column: 1 / -1; }
  .plan-row + .plan-row { margin-top: 14px; }
}
.plans-head { font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; }
.plan-row + .plan-row { margin-top: 8px; }
.field { position: relative; }
.field svg {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  color: var(--text-dim);
  pointer-events: none;
}
.field input { padding-left: 34px; }
.panelid { font-family: 'JetBrains Mono', monospace; }
.remove-plan { display: none; width: 30px; height: 30px; padding: 0; border-radius: 7px; color: var(--text-faint); }
.remove-plan svg { width: 15px; height: 15px; }
.remove-plan:hover { color: var(--bad); background: var(--bad-soft); }
.row.editing .remove-plan { display: inline-flex; }
.add-plan {
  display: none;
  margin-top: 8px;
  padding: 4px 0;
  font-size: 12.5px;
  color: var(--accent);
}
.add-plan:hover { text-decoration: underline; }
.row.editing .add-plan { display: inline-flex; }

.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 20px;
  border-left: 1px solid var(--border);
  align-self: stretch;
}
.icon-btn { width: 38px; height: 38px; padding: 0; border-radius: 8px; }
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn.save { background: var(--blue); color: #fff; }
.icon-btn.save:hover { background: var(--blue-hover); }
.row:not(.dirty) .icon-btn.save { opacity: .55; }
.icon-btn.edit { background: var(--surface-2); color: var(--text-dim); border-color: var(--border); }
.icon-btn.edit:hover, .row.editing .icon-btn.edit { color: var(--accent); border-color: var(--ring); background: var(--accent-soft); }
.icon-btn.delete { background: var(--bad-soft); color: var(--bad); }
.icon-btn.delete:hover { background: color-mix(in srgb, var(--bad) 25%, transparent); }

/* Grid view: stack each row's sections */
#cards.grid .row { grid-template-columns: 1fr auto; gap: 14px 16px; align-items: start; }
#cards.grid .row-plans { grid-column: 1 / -1; }
#cards.grid .actions { grid-column: 1 / -1; border-left: none; padding-left: 0; padding-top: 12px; border-top: 1px solid var(--border); justify-content: flex-end; }

/* ---- Empty / pagination ---- */
.empty {
  text-align: center;
  color: var(--text-dim);
  font-size: 13.5px;
  padding: 56px 20px;
  border: 1px dashed var(--border-strong);
  border-radius: 12px;
  line-height: 1.6;
}
.empty strong { display: block; color: var(--text); font-size: 15px; margin-bottom: 4px; }
#pagination { display: none; align-items: center; justify-content: center; gap: 14px; margin-top: 20px; }
#pagination button { padding: 7px 14px; font-size: 13px; }
#page-label { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }

/* ---- Toast ---- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  transition: opacity .2s, transform .2s;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { color: var(--bad); }

/* ---- Locked state: text stays readable, editing controls are disabled ---- */
.lock-banner {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
  border-radius: 10px;
  background: var(--accent-soft);
  border: 1px solid var(--ring);
  color: var(--text);
  font-size: 13.5px;
}
.lock-banner svg { width: 16px; height: 16px; color: var(--accent); }
.lock-banner .link { margin-left: auto; padding: 0; color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
body.locked .lock-banner { display: flex; }
body.locked #add-group-btn,
body.locked #add-form .btn-primary,
body.locked .icon-btn.save,
body.locked .icon-btn.edit,
body.locked .icon-btn.delete {
  opacity: .4;
  pointer-events: none;
}

/* ---- Responsive ---- */
@media (max-width: 1180px) {
  .stats-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #add-group-btn { grid-column: 1 / -1; }
  .row { grid-template-columns: minmax(0, 1fr) auto; gap: 14px 16px; align-items: start; }
  .row-plans { grid-column: 1 / -1; }
  .actions { grid-column: 1 / -1; border-left: none; padding-left: 0; padding-top: 12px; border-top: 1px solid var(--border); justify-content: flex-end; }
}
@media (max-width: 720px) {
  .shell { margin: 0; border-radius: 0; border-left: none; border-right: none; }
  header { padding: 16px; }
  .brand .sub { display: none; }
  .header-pill .label { display: none; }
  main { padding: 16px; }
  .toolbar { flex-wrap: wrap; }
  .search { flex-basis: 100%; max-width: none; }
  .toolbar .spacer { display: none; }
  .select { flex: 1; }
  .select select, .select.with-icon select { min-width: 0; }
  #cards.grid { grid-template-columns: 1fr; }
  .add-grid { grid-template-columns: 1fr; }
  .row { padding-left: 20px; }
  .ident { gap: 12px; }
  .avatar { width: 42px; height: 42px; }
}
