/* Igloo theme (Phase 9.10, ADR-021): a clean modern UI built around the icon's bright-to-deep teal gradient. Colours live only in
   these tokens; tests/theme-contrast.test.js checks every text pair against WCAG AA and tests/icon-palette.test.js ties --brand and
   --brand-bright to the icon. The auto-dark and manual-dark blocks must stay identical. */
:root {
  --bg: #f2f5f6;
  --surface: #ffffff;
  --hover: #e9eff1;
  --text: #14262a;
  --muted: #55696f;
  --border: #d9e1e4;
  --accent: #00706f;
  --brand: #008f8f;
  --brand-bright: #00c1ba;
  --fill: #00c1ba;
  --fill-end: #00a69f;
  --on-fill: #04262a;
  --accent-2: #d9482a;
  --mustard: #f2b134;
  --focus: #3a55d6;
  --sev-error: #b3261e;
  --sev-warn: #805000;
  --sev-info: #1b5a66;
  --hatch: #cdd8dc;
  --tile: #f4eee0;
  --shadow: rgb(16 38 44 / 0.14);
  --fill-gradient: linear-gradient(135deg, var(--fill), var(--fill-end));
  --overlay: #ffffff;
  --overlay-inset: #f2f5f6;
  --overlay-border: #c9d4d8;
  --font-heading: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b171a;
    --surface: #12242a;
    --hover: #1b3239;
    --text: #e8f0f2;
    --muted: #9db3b9;
    --border: #25393f;
    --accent: #3fe0d6;
    --brand: #00a9a2;
    --brand-bright: #2fe3d8;
    --fill: #00c1ba;
    --fill-end: #00a69f;
    --on-fill: #04262a;
    --accent-2: #ff8a5c;
    --mustard: #ffc552;
    --focus: #9fb0ff;
    --sev-error: #ff9a8a;
    --sev-warn: #f2c96a;
    --sev-info: #8fe0da;
    --hatch: #2a4147;
    --tile: #f4eee0;
    --shadow: rgb(0 0 0 / 0.5);
    --fill-gradient: linear-gradient(135deg, var(--fill), var(--fill-end));
    --overlay: #19313a;
    --overlay-inset: #0f1f24;
    --overlay-border: #38565e;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b171a;
  --surface: #12242a;
  --hover: #1b3239;
  --text: #e8f0f2;
  --muted: #9db3b9;
  --border: #25393f;
  --accent: #3fe0d6;
  --brand: #00a9a2;
  --brand-bright: #2fe3d8;
  --fill: #00c1ba;
  --fill-end: #00a69f;
  --on-fill: #04262a;
  --accent-2: #ff8a5c;
  --mustard: #ffc552;
  --focus: #9fb0ff;
  --sev-error: #ff9a8a;
  --sev-warn: #f2c96a;
  --sev-info: #8fe0da;
  --hatch: #2a4147;
  --tile: #f4eee0;
  --shadow: rgb(0 0 0 / 0.5);
  --fill-gradient: linear-gradient(135deg, var(--fill), var(--fill-end));
  --overlay: #19313a;
  --overlay-inset: #0f1f24;
  --overlay-border: #38565e;
  color-scheme: dark;
}

/* Shape and rhythm (not colours). */
:root {
  --radius: 10px;
  --radius-lg: 14px;
  --radius-pill: 999px;
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --elev-1: 0 1px 2px var(--shadow);
  --elev-2: 0 2px 6px var(--shadow), 0 12px 32px var(--shadow);
}

/* Overlay layer: popovers, the drawer and dialogs sit on their own background. Remapping the base tokens here restyles
   everything nested inside without touching it. */
.popover, dialog.drawer, dialog.action-dialog, dialog.about-dialog, dialog.help-dialog {
  --surface: var(--overlay);
  --bg: var(--overlay-inset);
  --border: var(--overlay-border);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  display: grid;
  grid-template-columns: 3.5rem 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "header header" "nav main";
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
body:has(.app-nav.is-expanded) { grid-template-columns: 11rem 1fr; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip-link { position: absolute; left: -999px; top: 0; padding: 0.5rem 0.75rem; background: var(--fill-gradient); color: var(--on-fill); z-index: 100; }
.skip-link:focus { left: 0; }

/* ---- header */
.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  min-height: 3rem;
  padding: 0.4rem var(--space-3);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 0.55rem; }
.brand-button { display: block; padding: 0; border: 0; background: none; border-radius: 10px; cursor: pointer; }
.brand-tile { display: block; padding: 3px; background: var(--tile); border-radius: 9px; box-shadow: inset 0 0 0 1px var(--border); }
.brand-logo { display: block; width: 32px; height: 32px; }
.app-title { margin: 0; font-size: 1.15rem; font-weight: 700; letter-spacing: 0.01em; line-height: 1; }
.brand-tagline { color: var(--muted); font-size: 0.8rem; }
@media (max-width: 62rem) { .brand-tagline { display: none; } }
.spacer { flex: 1; }
.last-fetched { color: var(--muted); font-size: 0.8rem; padding: 0.15rem 0.55rem; border: 1px solid var(--border); border-radius: var(--radius-pill); background: var(--bg); white-space: nowrap; }
.load-status { color: var(--muted); font-size: 0.8rem; }
.last-fetched { display: inline-flex; align-items: center; gap: 0.35rem; }
.last-fetched .stale-icon { display: none; width: 0.85rem; height: 0.85rem; }
.last-fetched.is-stale { border-color: var(--sev-warn); color: var(--sev-warn); font-weight: 600; }
.last-fetched.is-stale .stale-icon { display: inline-flex; }

.mode-switch { display: inline-flex; padding: 2px; gap: 2px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-pill); cursor: pointer; }
.mode-seg { padding: 0.15rem 0.7rem; border-radius: var(--radius-pill); color: var(--muted); font-size: 0.75rem; font-weight: 700; letter-spacing: 0.03em; }
.mode-switch[data-mode="mock"] [data-seg="mock"], .mode-switch[data-mode="live"] [data-seg="live"] { background: var(--fill-gradient); color: var(--on-fill); }

/* ---- controls */
button, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.3rem 0.75rem;
}
select { padding-right: 0.4rem; }
button { cursor: pointer; transition: background-color 0.12s, border-color 0.12s, color 0.12s; }
button:hover:not(:disabled):not(.brand-button):not([aria-disabled="true"]), select:hover:not(:disabled) { background: var(--hover); border-color: var(--muted); }
.btn:hover:not([aria-disabled="true"]), .drawer-close:hover, .popover-close:hover { background: var(--hover); border-color: var(--brand); }
select:disabled { color: var(--muted); }
.brand-button:hover { background: none; }

:root { --icon-sm: 1rem; --icon-md: 1.25rem; --icon-lg: 1.5rem; }
.icon { display: inline-flex; width: var(--icon-md); height: var(--icon-md); flex: none; }
.icon svg { width: 100%; height: 100%; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem; min-width: 2rem; height: 2rem; padding: 0 0.4rem; background: transparent; border-color: transparent; color: var(--muted); border-radius: 8px; }
.icon-button:hover:not(:disabled):not([aria-disabled="true"]) { background: var(--hover); border-color: transparent; color: var(--text); }
.icon-button.with-text { padding: 0 0.65rem; border-color: var(--border); background: var(--surface); color: var(--text); }
.icon-button-text { font-size: 0.8rem; font-weight: 600; }
.icon-button[aria-pressed="true"] { background: var(--fill-gradient); color: var(--on-fill); border-color: transparent; }
.icon-button:disabled { cursor: progress; color: var(--muted); opacity: 0.7; }
#refresh-btn[data-mode="cancel"] { color: var(--accent-2); }
@media (prefers-reduced-motion: no-preference) {
  .is-spinning .icon { animation: spin 0.9s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
}

/* Tooltips: one element, drawn by ui/components/tooltip.js for any [data-tip] (hover and keyboard focus). */
.tooltip { position: fixed; z-index: 1000; width: max-content; max-width: 20rem; padding: 0.3rem 0.55rem; background: var(--text); color: var(--bg); border-radius: 6px; box-shadow: var(--elev-1); font-size: 0.75rem; font-weight: 500; line-height: 1.3; text-align: left; white-space: normal; letter-spacing: 0; text-transform: none; pointer-events: none; }
.tooltip[hidden] { display: none; }

/* ---- navigation rail */
.app-nav {
  grid-area: nav;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2) 0.4rem;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: relative;
  z-index: 20;
}
.app-nav a, .nav-item {
  display: flex; align-items: center; gap: 0.7rem; width: 100%; height: 2.4rem; padding: 0 0.7rem;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted); font: inherit; font-weight: 600; text-decoration: none; text-align: left; cursor: pointer;
}
.app-nav a:hover, .nav-item:hover:not(:disabled) { background: var(--hover); color: var(--text); border-color: transparent; }
.app-nav a[aria-current="page"] { background: var(--hover); color: var(--accent); box-shadow: inset 3px 0 0 var(--brand); }
.nav-icon { display: inline-flex; flex: none; }
.nav-spacer { flex: 1; }
.app-nav:not(.is-expanded) .nav-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

#view { grid-area: main; padding: var(--space-4) 1.25rem; overflow: auto; min-height: 0; }
#view:focus { outline: none; }
#view .page-head { margin: 0 0 0.75rem; }
#view .page-head h2 { margin: 0; font-size: 1.25rem; }
.page-sub { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.85rem; }
h2, h3 { font-family: var(--font-heading); }
.placeholder { color: var(--muted); }

@media (max-width: 40rem) {
  body, body:has(.app-nav.is-expanded) { grid-template-columns: 1fr; grid-template-areas: "header" "nav" "main"; }
  .app-nav { flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 1px solid var(--border); padding: 0.25rem; }
  .app-nav a, .nav-item { width: auto; }
  .nav-spacer { display: none; }
  #nav-toggle { display: none; }
  .app-nav [data-tip]::after { top: calc(100% + 6px); left: 50%; transform: translateX(-50%); }
}

button:disabled { cursor: progress; color: var(--muted); }

.state { padding: 0.75rem 1rem; border: 1px dashed var(--border); border-radius: var(--radius); color: var(--muted); background: var(--surface); }

/* Message types (Phase 9.36): .state (nothing here), .notice (know or do something), .status-line (result of the last action) */
.notice { padding: 0.5rem 0.75rem; border: 1px solid var(--border); border-left-width: 4px; border-radius: var(--radius); background: var(--surface); }
.notice-error { border-left-color: var(--sev-error); font-weight: 700; }
.notice-warn { border-left-color: var(--sev-warn); }
.notice-info { border-left-color: var(--sev-info); }
.notice-ok { border-left-color: var(--sev-good, var(--accent)); }
.notice-slot:empty { display: none; }
.status-line { min-height: 1.3em; margin: 0.4rem 0 0; font-size: 0.85rem; }

[hidden] { display: none !important; }

/* Export split button and menu (Phase 9.16) */
.export-menu { position: relative; display: inline-flex; }
.export-main { border-top-right-radius: 0; border-bottom-right-radius: 0; border-color: var(--border); background: var(--surface); color: var(--text); padding: 0 0.5rem; }
.export-caret { min-width: 1.6rem; padding: 0 0.15rem; border-top-left-radius: 0; border-bottom-left-radius: 0; border-color: var(--border); background: var(--surface); color: var(--muted); margin-left: -1px; }
.export-main:hover:not(:disabled), .export-caret:hover:not(:disabled) { border-color: var(--muted); }
.export-caret .icon { width: 0.9rem; height: 0.9rem; }
.export-list { position: absolute; z-index: 60; top: calc(100% + 4px); right: 0; min-width: 14rem; margin: 0; padding: 0.3rem; list-style: none; background: var(--overlay); border: 1px solid var(--overlay-border); border-radius: var(--radius); box-shadow: var(--elev-2); }
.export-item { display: flex; align-items: center; gap: 0.5rem; width: 100%; padding: 0.4rem 0.55rem; border: 0; border-radius: 8px; background: transparent; text-align: left; font: inherit; }
.export-item:hover, .export-item:focus-visible { background: var(--hover); border: 0; }
.export-check { width: 1rem; color: var(--accent); font-weight: 700; }
.export-default { margin-left: auto; color: var(--muted); font-size: 0.72rem; }
[data-tip].show-tip::after { opacity: 1; transition: none; }
@media (max-width: 56rem) { .scenario select { max-width: 9rem; } .last-fetched { display: none; } }

/* alerts center (Phase 9.25) */
.alerts-wrap { position: relative; display: inline-flex; margin-right: 0.3rem; }
.alerts-anchor { position: relative; display: inline-flex; }
.alerts-badge { position: absolute; top: -5px; right: -9px; min-width: 0.9rem; height: 0.9rem; padding: 0 0.2rem; border-radius: 999px; font-size: 0.62rem; font-weight: 800; line-height: 0.9rem; text-align: center; color: #fff; background: var(--sev-info); pointer-events: none; border: 1.5px solid var(--surface); box-sizing: content-box; }
.alerts-badge[hidden] { display: none; }
.alerts-badge.is-error { background: #b3261e; }
.alerts-badge.is-warn { background: #805000; }
.alerts-badge.is-info { background: #1b5a66; }
.alerts-panel { position: absolute; z-index: 70; top: calc(100% + 6px); right: 0; width: min(26rem, calc(100vw - 2rem)); max-height: min(30rem, 70vh); overflow: auto; background: var(--overlay); border: 1px solid var(--overlay-border); border-radius: var(--radius); box-shadow: var(--elev-2); }
.alerts-panel[hidden] { display: none; }
.alerts-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.4rem 0.8rem; padding: 0.55rem 0.75rem; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--overlay); }
.alerts-tools { display: flex; flex-wrap: wrap; gap: 0.2rem 0.8rem; }
.alerts-tools button:disabled { opacity: 0.5; cursor: default; }
.alerts-list { list-style: none; margin: 0; padding: 0; }
.alert-item { padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); border-left: 4px solid var(--sev-info); }
.alert-item.is-error { border-left-color: var(--sev-error); }
.alert-item.is-warn { border-left-color: var(--sev-warn); }
.alert-item.is-read { opacity: 0.72; }
.alert-item:last-child { border-bottom: 0; }
.alert-head { display: flex; align-items: center; gap: 0.4rem; font-weight: 700; }
.alert-item.is-read .alert-title { font-weight: 500; }
.alert-message, .alert-fix { margin: 0.25rem 0 0; font-size: 0.85rem; overflow-wrap: anywhere; }
.alert-fix { color: var(--muted); }
.alert-actions { display: flex; flex-wrap: wrap; gap: 0.4rem 0.9rem; align-items: center; margin-top: 0.4rem; }
.alerts-empty { margin: 0; padding: 0.9rem 0.75rem; color: var(--muted); font-size: 0.9rem; }
@media (max-width: 640px) {
  .alerts-wrap { position: static; }
  .alerts-panel { left: 0.5rem; right: 0.5rem; width: auto; top: 100%; }
}

/* buttons by role (Phase 9.34) */
.btn-with-icon { display: inline-flex; align-items: center; gap: 0.4rem; }
.btn-with-icon .icon { width: var(--icon-sm); height: var(--icon-sm); }
.btn-primary { background: var(--fill-gradient); color: var(--on-fill); border-color: transparent; font-weight: 700; }
.btn-primary:hover:not(:disabled):not([aria-disabled="true"]) { background: var(--fill-gradient); border-color: transparent; filter: brightness(1.08); }
.btn-danger { color: var(--sev-error); border-color: var(--sev-error); }
.btn-danger:hover:not(:disabled):not([aria-disabled="true"]) { background: var(--hover); border-color: var(--sev-error); }
.icon-button.is-danger:hover:not(:disabled):not([aria-disabled="true"]) { color: var(--sev-error); }
.icon-button[aria-disabled="true"], .icon-button:disabled { opacity: 0.45; cursor: not-allowed; }

.dialog-x { position: absolute; top: 0.5rem; right: 0.5rem; z-index: 2; }
.about-dialog, .action-dialog { position: fixed; }

/* status marks and external links (Phase 9.34) */
.marked { display: inline-flex; align-items: center; gap: 0.3rem; }
.mark-ok { display: inline-flex; color: var(--sev-good, var(--accent)); }
.mark-ok .icon { width: var(--icon-sm); height: var(--icon-sm); }
.external-icon { width: 0.85em; height: 0.85em; vertical-align: -0.1em; }
.external-link { display: inline-flex; align-items: center; gap: 0.2rem; }

/* fold controls (Phase 9.34b): one chevron for every collapse and expand */
.fold-chevron { display: inline-flex; flex: none; width: var(--icon-sm); height: var(--icon-sm); color: var(--muted); transition: transform 0.12s; }
.fold-chevron .icon { width: 100%; height: 100%; }
details[open] > .fold-summary .fold-chevron, [aria-expanded="true"] > .fold-chevron { transform: rotate(90deg); }
.fold-summary { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; list-style: none; }
.fold-summary::-webkit-details-marker { display: none; }
.fold-summary:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
