/* ============================================================
   components — surfaces, controls, tables, states
   ============================================================ */

/* ---------------- surface layers ----------------
   PAGE > SECTION (borderless, spacing-led) > SURFACE (card)
   > INSET MODULE (well) > FLOATING CONTROL                    */

.section { margin-bottom: var(--s10); position: relative; }

.section-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: var(--s4); margin-bottom: var(--s4); flex-wrap: wrap;
}
.section-head .t h2 { margin-bottom: 2px; }
.section-head .t .micro { display: block; margin-bottom: 4px; }
.section-head .act { display: flex; gap: var(--s2); align-items: center; flex-wrap: wrap; }
/* controls that belong to the heading sit next to it rather than being flung
   to the far edge of a wide screen (spec §21) */
.section-head.tight-act { justify-content: flex-start; }
.section-head.tight-act .act { margin-left: var(--s5); }
@media (max-width: 719px) { .section-head.tight-act .act { margin-left: 0; } }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: var(--s5);
  position: relative;
}
.card.pad-lg { padding: var(--s6); }
.card.flush { padding: 0; overflow: hidden; }

.well {
  background: var(--inset);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-sm);
  padding: var(--s4);
}

/* corner tick — the terminal signature on data-bearing surfaces */
.ticked::after {
  content: ""; position: absolute; top: 10px; right: 10px;
  width: 14px; height: 14px; pointer-events: none;
  border-top: 1.5px solid var(--line-strong);
  border-right: 1.5px solid var(--line-strong);
  border-top-right-radius: 4px; opacity: .7;
}

/* ---------------- buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 7px; padding: 8px 15px; min-height: 36px;
  background: var(--inter); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--r-sm);
  font-size: var(--fs-sm); font-weight: 600; cursor: pointer;
  transition: background var(--t-fast), border-color var(--t-fast), transform var(--t-fast);
  white-space: nowrap;
}
.btn:hover { background: var(--raised); border-color: var(--line-strong); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn.primary {
  background: var(--mint); border-color: var(--mint);
  color: #06231A; font-weight: 700;
}
.btn.primary:hover { background: var(--mint-bright); border-color: var(--mint-bright); }
html[data-theme="light"] .btn.primary { color: #fff; }
html[data-theme="light"] .btn.primary:hover { background: var(--mint-deep); border-color: var(--mint-deep); }

.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--inter); color: var(--text); }
.btn.danger { color: var(--down); border-color: var(--down-dim); background: var(--down-dim); }
.btn.danger:hover { background: var(--down); color: #fff; border-color: var(--down); }
.btn.sm { min-height: 30px; padding: 4px 11px; font-size: var(--fs-xs); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; min-width: 36px;
  background: transparent; border: 1px solid transparent;
  border-radius: var(--r-sm); color: var(--text-2); cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
  position: relative;
}
.icon-btn:hover { background: var(--inter); color: var(--text); }
/* The visual box is 36px; this expander carries the hit region to 48px, which
   is the size spec §17 asks for on touch. Measured with elementFromPoint:
   at inset -5px the effective target came out 42px, so it is -6px. */
.icon-btn::before { content: ""; position: absolute; inset: -6px; }

/* ---------------- inputs ---------------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: var(--fs-xs); color: var(--text-2); font-weight: 600; }

.input, .select, textarea.input {
  background: var(--inset); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--r-sm);
  padding: 8px 12px; min-height: 38px; font-size: var(--fs-body);
  transition: border-color var(--t-fast), background var(--t-fast);
  width: 100%;
}
.input:focus, .select:focus, textarea.input:focus {
  outline: none; border-color: var(--mint);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--mint-dim);
}
.input::placeholder { color: var(--muted); }
.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 55%, calc(100% - 11px) 55%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-right: 30px;
}

.check { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; }
.check input { accent-color: var(--mint); width: 16px; height: 16px; }

/* toggle switch */
.switch { position: relative; width: 38px; height: 22px; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch i {
  position: absolute; inset: 0; border-radius: 20px;
  background: var(--inter); border: 1px solid var(--line-strong);
  transition: background var(--t-med); pointer-events: none;
}
.switch i::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--text-2); transition: transform var(--t-med), background var(--t-med);
}
.switch input:checked + i { background: var(--mint-deep); border-color: var(--mint-deep); }
.switch input:checked + i::after { transform: translateX(16px); background: #fff; }
.switch input:focus-visible + i { outline: 2px solid var(--mint); outline-offset: 2px; }

/* inline text-button — for "read more" / "show detail" disclosures */
.linkish {
  background: none; border: 0; padding: 0;
  font: inherit; font-size: var(--fs-xs); font-weight: 600;
  color: var(--mint); cursor: pointer;
  text-underline-offset: 3px;
}
.linkish:hover { text-decoration: underline; }
.linkish:focus-visible { outline: 2px solid var(--mint); outline-offset: 3px; border-radius: 3px; }

/* ---------------- chips / badges (only real statuses) ---------------- */
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px;
  font-size: var(--fs-xs); font-weight: 600;
  background: var(--inter); color: var(--text-2);
  border: 1px solid transparent;
}
.chip.mint { background: var(--mint-dim); color: var(--mint); }
html[data-theme="light"] .chip.mint { color: var(--mint-deep); }
.chip.up   { background: var(--up-dim); color: var(--up); }
.chip.down { background: var(--down-dim); color: var(--down); }
.chip.warn { background: var(--warn-dim); color: var(--warn); }
.chip.info { background: var(--info-dim); color: var(--info); }
.chip.ai   { background: var(--ai-dim); color: var(--ai); }
.chip.gold { background: var(--gold-dim); color: var(--gold); }
.chip.outline { background: transparent; border-color: var(--line-strong); }

.tag {
  display: inline-block; padding: 1px 7px;
  font-size: var(--fs-micro); font-weight: 700; letter-spacing: .12em;
  text-transform: uppercase; border-radius: 5px;
  background: var(--inter); color: var(--muted);
}
.tag.ai { background: var(--ai-dim); color: var(--ai); }
.tag.mint { background: var(--mint-dim); color: var(--mint); }
html[data-theme="light"] .tag.mint { color: var(--mint-deep); }
.tag.warn { background: var(--warn-dim); color: var(--warn); }
.tag.gold { background: var(--gold-dim); color: var(--gold); }

/* data freshness */
.fresh { display: inline-flex; align-items: center; gap: 6px; font-size: var(--fs-xs); color: var(--muted); }
.fresh .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.fresh.live .dot { background: var(--up); box-shadow: 0 0 0 3px var(--up-dim); }
.fresh.delayed .dot { background: var(--warn); }
.fresh.stale .dot, .fresh.cached .dot { background: var(--muted); }
.fresh.off .dot { background: var(--down); }

/* ---------------- verdict colors (engine ladder) ---------------- */
.v-strong-buy { color: var(--up); }
.v-buy { color: var(--mint); }
html[data-theme="light"] .v-buy { color: var(--mint-deep); }
.v-hold { color: var(--text-2); }
.v-caution { color: var(--warn); }
.v-sell { color: var(--down); }

/* ---------------- tabs ---------------- */
.tabs {
  display: flex; gap: 2px; align-items: center;
  border-bottom: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs > button {
  background: none; border: 0; cursor: pointer;
  padding: 10px 14px; font-size: var(--fs-sm); font-weight: 600;
  color: var(--muted); white-space: nowrap;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: color var(--t-fast);
}
.tabs > button:hover { color: var(--text); }
.tabs > button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--mint); }

/* segmented control (compact, for real alternatives only) */
.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--inset); border: 1px solid var(--line);
  border-radius: var(--r-sm);
}
.seg > button {
  border: 0; background: none; cursor: pointer;
  padding: 5px 12px; border-radius: calc(var(--r-sm) - 4px);
  font-size: var(--fs-xs); font-weight: 600; color: var(--muted);
  transition: background var(--t-fast), color var(--t-fast);
  min-height: 28px;
}
.seg > button:hover { color: var(--text); }
.seg > button:disabled { opacity: .38; cursor: not-allowed; }
.seg > button:disabled:hover { color: var(--muted); }
.seg > button[aria-selected="true"], .seg > button.active {
  background: var(--raised); color: var(--text);
  box-shadow: var(--shadow-1);
}

/* ---------------- tables ---------------- */
.tbl-wrap { overflow-x: auto; overscroll-behavior-x: contain; border-radius: var(--r-sm); }
table.tbl { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.tbl th {
  position: sticky; top: 0; z-index: 2;
  background: var(--surface);
  text-align: left; font-size: var(--fs-micro); font-weight: 700;
  letter-spacing: .1em; text-transform: uppercase; color: var(--muted);
  padding: 8px 12px; border-bottom: 1px solid var(--line-strong);
  white-space: nowrap;
}
.tbl th.num, .tbl td.num { text-align: right; }
.tbl th.sortable { cursor: pointer; user-select: none; }
.tbl th.sortable:hover { color: var(--text); }
.tbl th .sort-ind { opacity: .9; color: var(--mint); font-size: 9px; margin-left: 3px; }
.tbl td { padding: 10px 12px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
.tbl tbody tr { transition: background var(--t-fast); }
.tbl tbody tr:hover { background: var(--inter); }
.tbl tbody tr.clickable { cursor: pointer; }
.tbl tbody tr:focus-visible { outline: 2px solid var(--mint); outline-offset: -2px; }
.tbl .sym { font-weight: 700; letter-spacing: .01em; }
.tbl .sub { display: block; font-size: var(--fs-xs); color: var(--muted); font-weight: 400; }

/* ---------------- meters & bars ---------------- */
.meter { display: grid; grid-template-columns: minmax(90px, 150px) 1fr auto; gap: 10px; align-items: center; font-size: var(--fs-sm); }
.meter .bar { height: 6px; border-radius: 4px; background: var(--inter); overflow: hidden; }
.meter .bar > i {
  display: block; height: 100%; border-radius: 4px;
  background: var(--mint); transition: width .5s cubic-bezier(.3, .7, .3, 1);
}
.meter .bar > i.warn { background: var(--warn); }
.meter .bar > i.down { background: var(--down); }
.meter .bar > i.info { background: var(--info); }
.meter .val { font-weight: 600; }

/* score ring (SVG drawn by JS; this styles the text inside) */
.ring-wrap { position: relative; display: inline-grid; place-items: center; }
.ring-wrap .ring-num { position: absolute; text-align: center; }
.ring-wrap .ring-num b { display: block; font-size: 1.5rem; font-weight: 800; letter-spacing: -.02em; }
.ring-wrap .ring-num span { font-size: var(--fs-micro); color: var(--muted); letter-spacing: .1em; text-transform: uppercase; }

/* ---------------- skeletons ---------------- */
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
.skel {
  border-radius: var(--r-xs);
  background: linear-gradient(90deg, var(--inter) 25%, var(--raised) 50%, var(--inter) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
  min-height: 14px;
}
.skel.text { height: 13px; }
.skel.h-lg { height: 40px; }
.skel.block { height: 120px; }
.skel.chart { height: 300px; border-radius: var(--r-md); }
.skel-stack { display: grid; gap: 10px; }

/* ---------------- states ---------------- */
.state {
  display: grid; justify-items: center; text-align: center;
  gap: var(--s3); padding: var(--s10) var(--s6);
  border: 1px dashed var(--line-strong); border-radius: var(--r-md);
  color: var(--text-2);
}
/* compact variant for empty states that sit inside an already-titled card,
   where the card heading carries the context (spec §10) */
.state.compact { gap: 10px; padding: var(--s6) var(--s6) 26px; }
.state.compact p { max-width: 52ch; }
/* an empty state inside a card reads as an empty well, not a boxed subpanel —
   the dashed outline is only needed when the state IS the surface (spec §16) */
.card > .state, .card .an-pane > .state, .card > * > .state {
  border: 0; background: var(--inset);
}
.state .st-icon {
  width: 46px; height: 46px; display: grid; place-items: center;
  border-radius: 14px; background: var(--inter); color: var(--muted);
}
.state .st-icon.warn { background: var(--warn-dim); color: var(--warn); }
.state .st-icon.down { background: var(--down-dim); color: var(--down); }
.state .st-icon.mint { background: var(--mint-dim); color: var(--mint); }
.state h4 { font-size: var(--fs-h3); letter-spacing: .02em; }
.state p { font-size: var(--fs-xs); color: var(--muted); max-width: 46ch; }
.state .progress { width: min(260px, 80%); }

.progress { height: 6px; border-radius: 4px; background: var(--inter); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--mint-deep); border-radius: 4px; }

/* error banner inside a module */
.mod-error {
  display: flex; gap: 10px; align-items: flex-start;
  background: var(--down-dim); border: 1px solid transparent;
  border-radius: var(--r-sm); padding: 12px 14px;
  font-size: var(--fs-sm); color: var(--text-2);
}
.mod-error svg { color: var(--down); flex: 0 0 auto; margin-top: 1px; }

.notice {
  display: flex; gap: 10px; align-items: flex-start;
  border-radius: var(--r-sm); padding: 11px 14px;
  font-size: var(--fs-xs); line-height: 1.6; color: var(--text-2);
  background: var(--inter);
}
.notice.warn { background: var(--warn-dim); }
.notice.warn svg { color: var(--warn); }
.notice.info { background: var(--info-dim); }
.notice.info svg { color: var(--info); }
.notice.ai { background: var(--ai-dim); }
.notice.ai svg { color: var(--ai); }
.notice svg { flex: 0 0 auto; margin-top: 2px; color: var(--muted); }

/* ---------------- Kata artwork ---------------- */
img.kata-art {
  object-fit: contain;
  pointer-events: none; user-select: none;
  filter: drop-shadow(0 10px 28px rgba(0, 0, 0, .38));
}
html[data-theme="light"] img.kata-art { filter: drop-shadow(0 8px 20px rgba(20, 40, 32, .16)); }
.state img.kata-art { margin-bottom: 2px; }
/* art placed beside content (e.g. stress test) — never over data on phones */
.art-side { flex: 0 0 auto; margin-left: auto; align-self: flex-end; }
@media (max-width: 679px) { .art-side { display: none; } }
.tag img { border-radius: 3px; }

/* ---------------- toast ---------------- */
#toasts {
  position: fixed; bottom: calc(var(--tabbar-h) + 14px + env(safe-area-inset-bottom, 0px));
  left: 50%; transform: translateX(-50%);
  display: grid; gap: 8px; z-index: var(--z-toast);
  width: min(430px, calc(100vw - 32px));
  pointer-events: none;
}
@media (min-width: 900px) { #toasts { bottom: 20px; left: auto; right: 20px; transform: none; } }
.toast {
  pointer-events: auto;
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--raised); border: 1px solid var(--line-strong);
  border-radius: var(--r-md); padding: 11px 14px;
  box-shadow: var(--shadow-pop);
  font-size: var(--fs-sm);
  animation: toast-in .18s cubic-bezier(.3, .7, .4, 1);
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
.toast.out { opacity: 0; transform: translateY(6px); transition: all .22s ease; }
.toast svg { flex: 0 0 auto; margin-top: 1px; }
.toast.ok svg { color: var(--up); }
.toast.warn svg { color: var(--warn); }
.toast.err svg { color: var(--down); }

/* ---------------- dialog / sheet ---------------- */
.overlay {
  position: fixed; inset: 0; z-index: var(--z-modal);
  background: rgba(4, 8, 12, .6);
  backdrop-filter: blur(3px);
  display: grid; place-items: center;
  padding: var(--s4);
  animation: fade-in .16s ease;
}
html[data-theme="light"] .overlay { background: rgba(20, 40, 32, .32); }
@keyframes fade-in { from { opacity: 0; } }
.dialog {
  width: min(560px, 100%);
  max-height: min(86dvh, 720px);
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: var(--r-lg); box-shadow: var(--shadow-pop);
  display: flex; flex-direction: column;
  animation: dlg-in .2s cubic-bezier(.3, .7, .4, 1);
}
@keyframes dlg-in { from { opacity: 0; transform: translateY(10px) scale(.985); } }
.dialog > header {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--s3); padding: var(--s4) var(--s5);
  border-bottom: 1px solid var(--line);
}
.dialog > .dlg-body { padding: var(--s5); overflow-y: auto; }
.dialog > footer {
  display: flex; justify-content: flex-end; gap: var(--s2);
  padding: var(--s3) var(--s5); border-top: 1px solid var(--line);
}
@media (max-width: 599px) {
  .overlay { place-items: end center; padding: 0; }
  .dialog {
    width: 100%; max-height: 92dvh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    animation: sheet-in .24s cubic-bezier(.3, .7, .4, 1);
  }
  @keyframes sheet-in { from { transform: translateY(40px); opacity: .6; } }
}

/* ---------------- tooltip ---------------- */
.tip { position: relative; cursor: help; border-bottom: 1px dotted var(--muted); }
[data-tip] { position: relative; }
[data-tip]:hover::after, [data-tip]:focus-visible::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  width: max-content; max-width: 260px;
  background: var(--raised); color: var(--text-2);
  border: 1px solid var(--line-strong); border-radius: var(--r-xs);
  padding: 8px 11px; font-size: var(--fs-xs); font-weight: 400; line-height: 1.5;
  white-space: normal; z-index: var(--z-toast); box-shadow: var(--shadow-2);
  pointer-events: none;
}

kbd {
  font-family: var(--font-ui); font-size: var(--fs-micro);
  background: var(--inter); border: 1px solid var(--line-strong);
  border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px;
  color: var(--text-2);
}

/* ---------------- details / expandable ---------------- */
details.exp { border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface); }
/* an expander nested inside a card is a section of that card, not a card of
   its own — depth replaces the second outline (spec §16) */
.card details.exp, details.exp details.exp {
  border: 0; background: var(--inset);
}
details.exp > summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  padding: 13px 16px; font-weight: 600; font-size: var(--fs-sm);
  border-radius: var(--r-md);
}
details.exp > summary::-webkit-details-marker { display: none; }
details.exp > summary:hover { background: var(--inter); }
details.exp > summary .chev { margin-left: auto; transition: transform var(--t-med); color: var(--muted); flex: 0 0 auto; }
details.exp[open] > summary .chev { transform: rotate(180deg); }
details.exp > .exp-body { padding: 4px 16px 16px; }

/* row menu (⋯) */
.menu-wrap { position: relative; }
.menu-pop[hidden] { display: none; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: var(--z-drawer);
  min-width: 190px; padding: 5px;
  background: var(--raised); border: 1px solid var(--line-strong);
  border-radius: var(--r-md); box-shadow: var(--shadow-pop);
  display: grid; gap: 1px;
}
.menu-pop button {
  display: flex; align-items: center; gap: 9px;
  background: none; border: 0; cursor: pointer;
  padding: 9px 11px; border-radius: var(--r-xs);
  font-size: var(--fs-sm); color: var(--text-2); text-align: left;
}
.menu-pop button:hover { background: var(--inter); color: var(--text); }
.menu-pop button.danger { color: var(--down); }
.menu-pop button.danger:hover { background: var(--down-dim); }

/* live price flash */
@keyframes flash-up { 0% { background: var(--up-dim); } 100% { background: transparent; } }
@keyframes flash-down { 0% { background: var(--down-dim); } 100% { background: transparent; } }
.flash-up { animation: flash-up 1s ease-out; }
.flash-down { animation: flash-down 1s ease-out; }

/* generic grids */
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: var(--s4); }
/* cards end where their content ends instead of stretching to their row-mate */
.grid2.natural { align-items: start; }
.grid3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr)); gap: var(--s4); }
.kv { display: flex; justify-content: space-between; gap: var(--s4); padding: 7px 0; font-size: var(--fs-sm); border-bottom: 1px solid var(--line-soft); }
.kv:last-child { border-bottom: 0; }
.kv > span { color: var(--muted); }
.kv > b { font-weight: 600; text-align: right; }
