:root {
  --bg-0: #060a15;
  --bg-1: #0a0f1e;
  --rail-bg: #070b16;
  --card-bg: #0e1526;
  --card-bg-soft: #0b1220;
  --card-border: rgba(255, 255, 255, 0.07);
  --field-bg: rgba(255, 255, 255, 0.04);
  --field-border: rgba(255, 255, 255, 0.09);
  --text: #e8ecf7;
  --text-dim: #7c88a3;
  --text-faint: #4d5872;
  --accent: #4f8ff7;
  --accent-2: #9b6bff;
  --accent-teal: #22d3c0;
  --success: #34d399;
  --danger: #f87171;
  --radius-lg: 18px;
  --radius-md: 12px;
  --shadow-card: 0 10px 30px rgba(0, 0, 0, 0.25);
}

[data-theme="light"] {
  --bg-0: #eef1f8;
  --bg-1: #e6eaf4;
  --rail-bg: #ffffff;
  --card-bg: #ffffff;
  --card-bg-soft: #f4f6fb;
  --card-border: rgba(20, 30, 60, 0.08);
  --field-bg: #f4f6fb;
  --field-border: rgba(20, 30, 60, 0.12);
  --text: #171e33;
  --text-dim: #6b7590;
  --text-faint: #a2abc4;
  --shadow-card: 0 10px 26px rgba(30, 40, 80, 0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(1200px 700px at 90% -12%, rgba(79, 143, 247, 0.16), transparent 55%),
    radial-gradient(900px 620px at -8% 108%, rgba(155, 107, 255, 0.12), transparent 55%),
    linear-gradient(180deg, var(--bg-0), var(--bg-1));
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  transition: background 0.2s ease, color 0.2s ease;
}

.hidden { display: none !important; }
.muted { color: var(--text-dim); font-weight: 400; }
.muted-sm { color: var(--text-dim); font-size: 12px; margin: 2px 0 0; }
button { font-family: inherit; }
svg { display: block; }

/* ---------- Login ---------- */
.login-screen { height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  width: 320px; max-width: 92vw; padding: 28px;
  background: var(--card-bg); border: 1px solid var(--card-border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-card); text-align: center;
}
.login-card h1 { font-size: 18px; margin: 0 0 8px; }
.login-card p { color: var(--text-dim); margin: 0 0 16px; }
#login-username, #login-password { width: 100%; margin-bottom: 12px; }
.error-text { color: var(--danger); font-size: 12px; margin-top: 10px; }

/* ---------- Branding: uploaded logo ---------- */
.brand-logo { width: 42px; height: 42px; border-radius: 12px; object-fit: contain; flex: none; }
.brand-logo-lg { width: 64px; height: 64px; object-fit: contain; margin: 0 auto 14px; border-radius: 16px; }
.logo-lg { width: 60px; height: 60px; font-size: 26px; margin: 0 auto 14px; }
.login-card .brand-logo-lg { display: block; }

/* ---------- Maintenance ---------- */
.maintenance-card { width: 380px; max-width: 92vw; padding: 34px 30px; }
.maintenance-card h1 { font-size: 20px; }
.maintenance-card p { line-height: 1.55; white-space: pre-line; }
.maint-staff { margin-top: 6px; font-size: 11.5px; color: var(--text-faint); }
.maint-staff:hover { color: var(--text-dim); }
.maint-banner {
  flex: none; padding: 7px 16px; text-align: center; font-size: 12px; font-weight: 600;
  background: rgba(245,185,66,0.16); color: #f5b942; border-bottom: 1px solid rgba(245,185,66,0.3);
}

/* ---------- Notifications (bell) ---------- */
.notif-wrap { position: relative; }
#notif-btn { position: relative; }
.notif-badge {
  position: absolute; top: -5px; right: -5px; min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 800;
  display: flex; align-items: center; justify-content: center; border: 2px solid var(--card-bg-soft);
}
.notif-panel {
  position: absolute; top: calc(100% + 10px); right: 0; width: 340px; max-width: 92vw;
  background: var(--card-bg); border: 1px solid var(--card-border);
  border-radius: var(--radius-md); box-shadow: var(--shadow-card); z-index: 30; overflow: hidden;
}
.notif-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--card-border); font-size: 13px; }
.notif-readall:hover { color: var(--accent); }
.notif-list { max-height: 380px; overflow-y: auto; }
.notif-empty { padding: 22px 14px; text-align: center; }
.notif-item { display: flex; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--card-border); cursor: pointer; }
.notif-item:last-child { border-bottom: none; }
.notif-item:hover { background: var(--field-bg); }
.notif-item.unread { background: rgba(79,143,247,0.07); }
.notif-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; flex: none; background: var(--text-faint); }
.notif-item.unread .notif-dot { background: var(--accent); }
.notif-item.k-success.unread .notif-dot { background: var(--success); }
.notif-item.k-warning.unread .notif-dot { background: #f5b942; }
.notif-body { min-width: 0; flex: 1; }
.notif-title { font-size: 13px; font-weight: 700; }
.notif-msg { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; line-height: 1.45; white-space: pre-line; overflow-wrap: anywhere; }
.notif-time { font-size: 11px; color: var(--text-faint); margin-top: 4px; }

/* ---------- Topbar ---------- */
.app-shell { height: 100vh; display: flex; flex-direction: column; }
.topbar {
  height: 76px; min-height: 76px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--card-border);
  background: var(--card-bg-soft);
}
.brand-block { display: flex; align-items: center; gap: 12px; flex: none; }
.logo {
  width: 42px; height: 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-size: 18px;
  box-shadow: 0 6px 18px rgba(79, 143, 247, 0.35);
}
.brand-block h1 { font-size: 17px; margin: 0; font-weight: 700; }
.accent-text { background: linear-gradient(90deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.subtitle { margin: 1px 0 0; font-size: 12px; color: var(--text-dim); }

.badge-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 13px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: rgba(34, 211, 192, 0.14); color: var(--accent-teal);
  border: 1px solid rgba(34, 211, 192, 0.3);
}
.badge-pill.small { padding: 4px 10px; font-size: 11px; background: rgba(79,143,247,0.14); color: var(--accent); border-color: rgba(79,143,247,0.3); }
.count-pill {
  display: inline-flex; align-items: center; height: 38px; padding: 0 14px;
  border-radius: 10px; background: var(--field-bg); border: 1px solid var(--field-border);
  color: var(--text); font-size: 12.5px; font-weight: 600; white-space: nowrap;
}
.tagline { color: var(--text-dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar-gap { flex: 1; display: flex; align-items: center; justify-content: center; gap: 14px; min-width: 0; padding: 0 16px; }
.ai-badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 14px; border-radius: 999px; font-size: 12px; font-weight: 700;
  color: var(--accent-2);
  background: linear-gradient(90deg, rgba(79,143,247,0.14), rgba(155,107,255,0.14));
  border: 1px solid rgba(155, 107, 255, 0.3);
}
.ai-badge svg { width: 14px; height: 14px; }

.topbar-right { display: flex; align-items: center; gap: 10px; flex: none; }
.topbar-right .btn { flex: none; white-space: nowrap; text-decoration: none; }
/* Every direct child (theme toggle, credits pill, notif wrap, avatar wrap) keeps
   its natural width instead of being squeezed below its content on narrow
   screens — that shrinking was what let the credits text spill over the bell icon. */
.topbar-right > * { flex-shrink: 0; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: var(--field-bg); border: 1px solid var(--field-border);
  color: var(--text-dim); cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--text); background: rgba(255,255,255,0.08); }
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn-credits {
  width: auto; padding: 0 14px; gap: 8px; font: inherit; font-size: 13px; font-weight: 600;
  color: var(--text-dim); white-space: nowrap;
}
.icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  border: none; cursor: pointer;
}
.avatar-wrap { position: relative; }
.avatar-sm { width: 30px; height: 30px; font-size: 12px; flex: none; }
.profile-menu {
  position: absolute; top: calc(100% + 10px); right: 0; width: 220px;
  background: var(--card-bg); border: 1px solid var(--card-border);
  border-radius: var(--radius-md); box-shadow: var(--shadow-card);
  padding: 8px; z-index: 30;
}
.profile-menu-head { display: flex; align-items: center; gap: 10px; padding: 6px 8px 10px; }
.profile-name { font-size: 13px; font-weight: 700; }
.profile-sub { font-size: 11px; color: var(--text-dim); }
.profile-menu-divider { height: 1px; background: var(--card-border); margin: 6px 0; }
.profile-menu-item {
  width: 100%; display: flex; align-items: center; gap: 10px;
  background: none; border: none; color: var(--text); font-size: 13px;
  padding: 9px 8px; border-radius: 8px; cursor: pointer; text-align: left;
}
.profile-menu-item svg { width: 16px; height: 16px; flex: none; }
.profile-menu-item:hover { background: var(--field-bg); }
.profile-menu-item.danger { color: var(--danger); }

/* ---------- Body row / rail ---------- */
.body-row { flex: 1; display: flex; min-height: 0; }
.rail {
  width: 84px; min-width: 84px;
  background: var(--rail-bg); border-right: 1px solid var(--card-border);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 20px 10px;
}
.rail-btn {
  width: 100%; padding: 10px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: none; border: none; border-radius: 12px; cursor: pointer;
  color: var(--text-dim); font-size: 11px;
}
.rail-btn { text-align: center; }
.rail-btn span { display: block; width: 100%; text-align: center; line-height: 1.25; overflow-wrap: break-word; }
.rail-btn svg { width: 20px; height: 20px; flex: none; }
.rail-btn:hover { background: var(--field-bg); color: var(--text); }
.rail-btn.active { background: linear-gradient(135deg, rgba(79,143,247,0.18), rgba(155,107,255,0.18)); color: var(--accent); }
.rail-links { display: contents; } /* lets each dynamically-added link render as its own direct rail item */
a.rail-btn { text-decoration: none; }

/* ---------- Left settings panel ---------- */
.panel { width: 300px; min-width: 300px; border-right: 1px solid var(--card-border); }
.panel-scroll { height: 100%; overflow-y: auto; padding: 20px; }
.panel-title { font-size: 15px; margin: 0 0 6px; font-weight: 700; }

.settings-section {
  padding: 12px 14px 14px; margin-bottom: 12px; border-radius: var(--radius-md);
  background: var(--card-bg-soft); border: 1px solid var(--card-border);
  display: flex; flex-direction: column; gap: 12px;
}
.field-group { display: flex; flex-direction: column; gap: 6px; }
.settings-section .field-label { margin: 0; }
.section-title {
  margin: 0 0 2px; font-size: 11px; font-weight: 800; text-transform: uppercase;
  letter-spacing: 0.5px; color: var(--accent);
}

.field-label {
  display: flex; align-items: center; gap: 7px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
  color: var(--text-dim); margin: 0;
}

/* ---------- Segmented button groups (Glass Style, Highlight Intensity) ---------- */
.seg-group { display: flex; gap: 8px; }
.seg-btn {
  flex: 1; padding: 10px 6px; border-radius: 9px; text-align: center;
  border: 1px solid var(--field-border); background: var(--field-bg);
  color: var(--text-dim); font-size: 12.5px; font-weight: 600; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease, transform 0.08s ease;
}
.seg-btn:hover { color: var(--text); border-color: rgba(255,255,255,0.2); }
[data-theme="light"] .seg-btn:hover { border-color: rgba(20,30,60,0.25); }
.seg-btn:active { transform: scale(0.97); }
.seg-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent; color: #fff;
}

/* ---------- Background color swatch grid ---------- */
.swatch-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.swatch {
  aspect-ratio: 1; min-width: 0; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--field-border);
  display: flex; align-items: center; justify-content: center; font-size: 16px;
  transition: border-color 0.12s ease, transform 0.08s ease, box-shadow 0.12s ease;
}
.swatch:hover { transform: scale(1.05); }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(79,143,247,0.22); }
.swatch.swatch-random { background: var(--field-bg); }
.chip { width: 8px; height: 8px; border-radius: 3px; display: inline-block; }
.chip-a { background: var(--accent-2); }
.chip-b { background: var(--accent); }
.chip-c { background: var(--accent-teal); }
.chip-d { background: #f59e0b; }
.chip-e { background: #f472b6; }

.field {
  width: 100%; padding: 9px 11px; border-radius: 9px;
  border: 1px solid var(--field-border); background: var(--field-bg);
  color: var(--text); font-size: 13px; outline: none;
}
.field:focus { border-color: var(--accent); }
/* Native <option> list follows the OS/browser by default and ignores the
   dark field background, which made dropdowns show barely-readable grey
   text on white. Force option colors so every dropdown matches the theme. */
select.field { cursor: pointer; }
select.field option {
  background: var(--card-bg);
  color: var(--text);
  padding: 6px;
}
select.field option:checked,
select.field option:hover { background: var(--accent); color: #fff; }
.textarea { resize: vertical; font-family: inherit; }
.char-count { text-align: right; font-size: 11px; color: var(--text-faint); margin-top: 3px; }
.row { display: flex; gap: 8px; }
.row .field { flex: 1; }

.collapsible-toggle {
  width: 100%; display: flex; align-items: center; gap: 8px;
  background: none; border: none; color: var(--text); font-size: 12px; font-weight: 700;
  cursor: pointer; padding: 14px 0 6px; text-transform: uppercase; letter-spacing: 0.4px;
}
.collapsible-toggle.no-pad { padding: 0; text-transform: none; font-size: 13px; }
.chevron { width: 14px; height: 14px; transition: transform 0.15s ease; }
.collapsible-toggle.open .chevron { transform: rotate(180deg); }
.collapsible-panel { padding-top: 4px; }
.link-btn { background: none; border: none; color: var(--text-dim); font-size: 12px; cursor: pointer; }
.link-btn:hover { color: var(--danger); }

.actions { margin-top: 22px; display: flex; flex-direction: column; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 14px; border-radius: 10px;
  border: 1px solid var(--field-border); background: var(--field-bg);
  color: var(--text); font-size: 13px; font-weight: 600; cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn svg { width: 15px; height: 15px; }
.btn:hover { background: rgba(255,255,255,0.08); }
[data-theme="light"] .btn:hover { background: rgba(20,30,60,0.06); }
.btn:active { transform: scale(0.98); }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; }
.btn-accent { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border: none; color: #fff; }
.btn-accent:hover { background: linear-gradient(135deg, var(--accent), var(--accent-2)); filter: brightness(1.12); }
.btn-sm { padding: 8px 12px; font-size: 12px; flex: 1; }
.btn-sm.icon-only { flex: 0 0 auto; padding: 8px 10px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Content ---------- */
.content, .right-col { overflow-y: hidden; padding: 20px; display: flex; flex-direction: column; gap: 18px; }
.content { flex: 1; min-width: 0; }
.right-col { width: 320px; min-width: 320px; border-left: 1px solid var(--card-border); }

/* Center column: results grid fills all available height and scrolls internally. */
.results-card { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.results-card .grid { flex: 1; overflow-y: auto; align-content: start; }

/* Right column: history fills the whole column. */
.history-card { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.history-card .history-list { flex: 1; min-height: 0; }

.card {
  background: var(--card-bg); border: 1px solid var(--card-border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-card);
  padding: 16px 18px;
}
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-header-title { display: flex; align-items: center; gap: 10px; }
.title-icon {
  width: 34px; height: 34px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, rgba(79,143,247,0.2), rgba(155,107,255,0.2));
  color: var(--accent); font-size: 15px;
}
.card-header h2 { margin: 0; font-size: 15px; }
.small-title { margin: 0; font-size: 14px; }
.toolbar { display: flex; gap: 6px; align-items: center; }

.log {
  background: rgba(0,0,0,0.22); border: 1px solid var(--card-border); border-radius: 10px;
  padding: 10px 12px; height: 130px; overflow-y: auto;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11.5px;
  color: var(--text-dim); white-space: pre-wrap;
}
[data-theme="light"] .log { background: rgba(20,30,60,0.04); }
.log-small { height: 90px; }

/* ---------- Generated results grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.res-card {
  aspect-ratio: 1/1;
  border-radius: 10px;
  background: #0a0f1e;
  border: 1px solid var(--field-border);
  /* Establishes a new stacking + containing-block context for this card, so nothing
     inside it (including a stray position:fixed element from injected content) can
     ever render or be clicked outside this box. This is what keeps the delete
     button pinned to its own card no matter what. */
  position: relative;
  isolation: isolate;
  contain: layout paint;
}
[data-theme="light"] .res-card { background: #f4f6fb; }
.res-thumb {
  position: absolute; inset: 0;
  border-radius: inherit;
  overflow: hidden;
  cursor: pointer;
}
.res-thumb svg, .res-thumb img { display: block; width: 100%; height: 100%; }
.res-thumb .spinner { position: absolute; inset: 0; margin: auto; width: 22px; height: 22px; border: 3px solid var(--field-border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
.res-card .status-chip {
  position: absolute; bottom: 5px; left: 5px; font-size: 9px; padding: 2px 7px; border-radius: 999px;
  background: rgba(0,0,0,0.55); color: var(--success); z-index: 2; pointer-events: none;
}
.res-card .status-chip.failed { color: var(--danger); }
.res-card .status-chip.generating { color: var(--accent); }
.res-del {
  position: absolute; top: 5px; right: 5px; z-index: 3;
  width: 20px; height: 20px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.55); color: #fff; border: none; font-size: 11px; line-height: 1; cursor: pointer;
}
.res-del:hover { background: rgba(0,0,0,0.75); }
@keyframes spin { to { transform: rotate(360deg); } }

.history-list { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.history-empty { padding: 6px 0; }
.history-row { display: flex; align-items: center; gap: 10px; cursor: pointer; padding: 6px; border-radius: 10px; }
.history-row:hover { background: var(--field-bg); }
.history-thumb { width: 44px; height: 44px; border-radius: 8px; overflow: hidden; flex: none; background: #0a0f1e; }
[data-theme="light"] .history-thumb { background: #f4f6fb; }
.history-thumb svg { width: 100%; height: 100%; }
.history-info { min-width: 0; flex: 1; }
.history-info .h-name { font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-info .h-time { font-size: 11px; color: var(--text-dim); }
.history-tag { font-size: 10px; padding: 2px 8px; border-radius: 999px; background: rgba(79,143,247,0.14); color: var(--accent); white-space: nowrap; }
.history-tag.failed { background: rgba(239,68,68,0.14); color: var(--danger); }
.history-tag.generating { background: rgba(79,143,247,0.14); color: var(--accent); }
.history-hint { font-size: 11px; }
.history-modal-list { max-height: 60vh; overflow-y: auto; padding-right: 4px; }

.res-retry {
  position: absolute; right: 6px; bottom: 6px; z-index: 2;
  background: rgba(0,0,0,0.65); color: #fff; border: none; border-radius: 8px;
  font-size: 11px; padding: 5px 9px; cursor: pointer;
}
.res-retry:hover { background: rgba(0,0,0,0.85); }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(4,6,14,0.6); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card {
  position: relative; width: 420px; max-width: 92vw; max-height: 86vh; overflow-y: auto;
  background: var(--card-bg); border: 1px solid var(--card-border); border-radius: var(--radius-lg);
  padding: 24px; box-shadow: var(--shadow-card);
}
.modal-wide { width: 760px; }
.modal-narrow { width: 340px; }
.modal-close { position: absolute; top: 14px; right: 14px; background: none; border: none; color: var(--text-dim); font-size: 20px; cursor: pointer; }
.modal-close:hover { color: var(--text); }
.modal-body-row { display: flex; gap: 20px; }
.detail-preview { flex: 1; min-width: 0; background: #0a0f1e; border-radius: 10px; display: flex; align-items: center; justify-content: center; aspect-ratio: 1/1; }
[data-theme="light"] .detail-preview { background: #f4f6fb; }
.detail-preview svg { width: 100%; height: 100%; }
.detail-side { width: 260px; display: flex; flex-direction: column; gap: 10px; }
.detail-side h3 { margin: 0 0 4px; font-size: 14px; }

.provider-list { display: flex; flex-direction: column; gap: 8px; margin: 14px 0; max-height: 360px; overflow-y: auto; }
.provider-row {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px 10px; border-radius: 10px;
  background: var(--field-bg); border: 1px solid var(--field-border);
}
.provider-row.active { border-color: var(--accent); }
.provider-row-head { display: flex; align-items: center; gap: 6px; }
.provider-tag { font-size: 10.5px; font-weight: 700; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.provider-active-tag {
  font-size: 9.5px; font-weight: 700; padding: 1px 7px; border-radius: 999px;
  background: rgba(52, 211, 153, 0.16); color: var(--success);
}
.provider-del {
  margin-left: auto; width: 18px; height: 18px; border-radius: 50%;
  background: none; border: none; color: var(--text-dim); font-size: 11px;
  cursor: pointer; line-height: 1; flex: none;
}
.provider-del:hover { color: var(--danger); }
.provider-row-body { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.provider-row-body .field { padding: 6px 8px; font-size: 12px; }
.provider-row-actions { display: flex; gap: 6px; }
.provider-row-actions .btn { flex: 1; padding: 5px 10px; font-size: 11.5px; }
.add-provider-row { display: flex; gap: 8px; }
.add-provider-row .field { flex: 1; }

/* ---- Provider health summary + status badges ---- */
.provider-summary {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border-radius: 10px;
  background: var(--field-bg); border: 1px solid var(--field-border);
}
.summary-stats { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--text-dim); }
.summary-stats b { color: var(--text); font-weight: 700; }
.summary-stats .s-live b { color: var(--success); }
.summary-stats .s-dead b { color: var(--danger); }
.summary-stats .s-limit b { color: #f5b942; }

.status-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  white-space: nowrap; cursor: default;
}
.status-badge.st-live { background: rgba(52, 211, 153, 0.16); color: var(--success); }
.status-badge.st-dead { background: rgba(248, 113, 113, 0.16); color: var(--danger); }
.status-badge.st-limit { background: rgba(245, 185, 66, 0.16); color: #f5b942; }
.status-badge.st-error { background: rgba(245, 185, 66, 0.16); color: #f5b942; }
.status-badge.st-nokey { background: rgba(148, 163, 184, 0.14); color: var(--text-dim); }
.status-badge.st-legacy { background: rgba(148, 163, 184, 0.14); color: var(--text-dim); }
.status-badge.st-unchecked { background: rgba(148, 163, 184, 0.14); color: var(--text-dim); }
.status-badge.st-checking { background: rgba(79,143,247,0.14); color: var(--accent); }
.provider-check-btn { margin-left: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px);
  max-width: 460px; padding: 11px 18px; border-radius: 12px; font-size: 13px;
  background: var(--card-bg); color: var(--text); border: 1px solid rgba(248,113,113,0.4);
  box-shadow: var(--shadow-card); opacity: 0; pointer-events: none; z-index: 80;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.success { border-color: rgba(52,211,153,0.45); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1180px) {
  .right-col { width: 280px; min-width: 280px; }
}
@media (max-width: 1100px) {
  .tagline { display: none; }
}

/* ---------- Tablet: stack the 3-column layout, let the page scroll as one
   piece instead of nesting nine little scroll boxes inside each other ---------- */
@media (max-width: 980px) {
  html, body { height: auto; min-height: 100%; }
  .app-shell { height: auto; min-height: 100vh; }
  .body-row { flex-direction: column; height: auto; overflow: visible; }

  /* Topbar: give up the decorative "AI Powered" badge and the subtitle line
     to make room, and let it wrap onto a second line as a fallback instead
     of clipping — this is the row that was overflowing on iPad-width screens. */
  .topbar { flex-wrap: wrap; height: auto; min-height: 64px; padding: 10px 16px; row-gap: 8px; }
  .subtitle { display: none; }
  .topbar-gap { display: none; }

  /* Nav rail becomes a swipeable icon strip pinned to the top of the stack,
     rather than wrapping (and clipping) across two lines. */
  .rail {
    width: 100%; min-width: 0; flex-direction: row; justify-content: flex-start;
    flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch;
    padding: 8px 10px; gap: 4px; scrollbar-width: none;
  }
  .rail::-webkit-scrollbar { display: none; }
  .rail-btn {
    width: auto; flex: none; flex-direction: row; padding: 8px 14px;
    white-space: nowrap;
  }
  .rail-btn span { width: auto; }

  .panel, .content, .right-col {
    width: 100%; min-width: 0; border: none; border-bottom: 1px solid var(--card-border);
    overflow: visible; height: auto;
  }
  .panel-scroll { height: auto; overflow-y: visible; }

  /* Each stacked section keeps its own natural height instead of fighting
     for a flex:1 share of a viewport that no longer applies. */
  .results-card, .history-card { flex: none; min-height: 0; }
  .results-card .grid, .history-card .history-list { flex: none; overflow-y: visible; max-height: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }

  .modal-wide { width: 92vw; }
  .modal-body-row { flex-direction: column; }
  .detail-side { width: 100%; }
  .detail-preview { aspect-ratio: 4/3; }
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
  body { font-size: 13.5px; }

  .topbar {
    padding: 10px 12px; gap: 8px;
  }
  .brand-block { gap: 8px; }
  .logo, .brand-logo { width: 34px; height: 34px; }
  .brand-block h1 { font-size: 14.5px; }
  .topbar-right { gap: 6px; flex-wrap: wrap; row-gap: 6px; justify-content: flex-end; }
  /* Exclude the credits pill: it needs width:auto to fit "196 credits" —
     this rule used to also match it (equal specificity, later in the file)
     and forced it back down to a 34px square, which squeezed the text out
     over the notification bell. */
  .icon-btn:not(.icon-btn-credits) { width: 34px; height: 34px; }
  .icon-btn-credits { height: 34px; }
  .icon-btn-credits { padding: 0 10px; font-size: 12px; }
  .notif-panel { width: min(92vw, 320px); }
  .profile-menu { width: min(92vw, 220px); }

  .settings-section { padding: 12px 12px 14px; gap: 12px; margin-bottom: 12px; }
  .content, .right-col, .panel-scroll { padding: 14px; gap: 14px; }
  .card { padding: 14px; }
  .swatch-grid { gap: 8px; }
  .seg-group { flex-wrap: wrap; }
  .seg-btn { flex: 1 1 calc(50% - 8px); }

  .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
  .card-header { flex-wrap: wrap; gap: 8px; }
  .toolbar { flex-wrap: wrap; }

  .modal-card { padding: 16px; max-height: 90vh; }
  .modal-wide { width: 94vw; }
  .provider-row-body { grid-template-columns: 1fr; }
  .add-provider-row { flex-direction: column; }
  .add-provider-row .btn { width: 100%; }

  .toast { width: 92vw; max-width: 360px; left: 50%; }
}

/* ---------- Small phones ---------- */
@media (max-width: 420px) {
  .topbar-right { flex-wrap: wrap; justify-content: flex-end; }
  .rail-btn { padding: 7px 10px; font-size: 10.5px; }
  .rail-btn svg { width: 18px; height: 18px; }
  .swatch-grid { grid-template-columns: repeat(4, 1fr); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .count-pill { padding: 0 10px; font-size: 11.5px; }
}
