:root {
  --bg: #07080b;
  --panel: #10141a;
  --line: #1d2630;
  --text: #e8edf2;
  --muted: #7d8b99;
  --green: #3ee0a0;
  --yellow: #e6c35c;
  --orange: #ff8a3c;
  --red: #ff4d55;
  --alert: #ff2a6d;
  --pad: 28px;
  --gap: 14px;
  --tap: 44px;
  --footer-h: 52px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  letter-spacing: 0.01em;
  padding-bottom: calc(var(--footer-h) + var(--safe-b) + 16px);
  overflow-x: hidden;
}
img, canvas, svg { max-width: 100%; }
button, a, input, select, textarea { touch-action: manipulation; }
button { min-height: 36px; }

.grain {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.04;
  z-index: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8'/%3E%3C/filter%3E%3Crect width='80' height='80' filter='url(%23n)'/%3E%3C/svg%3E");
}

.top, .tabs, main, footer { position: relative; z-index: 1; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  padding: calc(22px + var(--safe-t)) max(var(--pad), var(--safe-r)) 12px max(var(--pad), var(--safe-l));
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; gap: 14px; align-items: center; min-width: 0; }
.mark {
  font-family: "IBM Plex Mono", monospace;
  font-weight: 600;
  font-size: 22px;
  border: 1px solid var(--orange);
  color: var(--orange);
  padding: 8px 10px;
  line-height: 1;
  flex-shrink: 0;
}
h1 { margin: 0; font-size: 20px; font-weight: 600; line-height: 1.2; }
.sub {
  margin: 4px 0 0;
  color: var(--muted);
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
  letter-spacing: 0.04em;
}
.top-meta { display: flex; gap: 12px; align-items: center; flex-shrink: 0; }
.clock { font-family: "IBM Plex Mono", monospace; color: var(--muted); font-size: 12px; white-space: nowrap; }
.toast {
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
  color: var(--orange);
  min-height: 1em;
  max-width: 220px;
}

button {
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  padding: 8px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: wait; }
button.textish { border: none; color: var(--muted); padding: 0; min-height: var(--tap); }
#refreshBtn { min-height: var(--tap); white-space: nowrap; }

.tabs {
  display: flex;
  gap: 0;
  padding: 0 max(16px, var(--safe-r)) 0 max(16px, var(--safe-l));
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  border: none;
  border-bottom: 2px solid transparent;
  padding: 12px 16px;
  color: var(--muted);
  min-height: var(--tap);
  flex: 0 0 auto;
  white-space: nowrap;
}
.tabs button.active {
  color: var(--text);
  border-bottom-color: var(--orange);
}

main {
  padding: 22px max(var(--pad), var(--safe-r)) 24px max(var(--pad), var(--safe-l));
  max-width: 1280px;
  width: 100%;
}

.banner {
  border: 1px solid #3a2a12;
  background: #16110b;
  color: var(--yellow);
  font-family: "IBM Plex Mono", monospace;
  font-size: 12px;
  line-height: 1.45;
  padding: 10px 14px;
  margin-bottom: 16px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
  gap: var(--gap);
}
.grid-2 {
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  gap: var(--gap);
  margin-top: var(--gap);
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 16px 16px 12px;
  min-width: 0;
}
.head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}
.sector-name { font-size: 12px; color: var(--muted); font-family: "IBM Plex Mono", monospace; }
.score {
  font-family: "IBM Plex Mono", monospace;
  font-size: 36px;
  line-height: 1;
  font-weight: 600;
}
.band { font-family: "IBM Plex Mono", monospace; font-size: 14px; font-weight: 600; }
.action { margin-top: 8px; color: var(--muted); font-size: 12px; font-family: "IBM Plex Mono", monospace; }

.ring {
  --p: 0;
  --c: var(--muted);
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: conic-gradient(var(--c) calc(var(--p) * 1%), #1c232c 0);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.ring span {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--panel);
  display: grid;
  place-items: center;
  font-family: "IBM Plex Mono", monospace;
  font-size: 13px;
  font-weight: 600;
}

.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}
.kpi {
  border: 1px solid var(--line);
  padding: 10px 12px;
  background: #0c1015;
  min-width: 0;
}
.kpi b {
  display: block;
  font-family: "IBM Plex Mono", monospace;
  font-size: clamp(16px, 2.4vw, 22px);
  overflow-wrap: anywhere;
}
.kpi small {
  color: var(--muted);
  font-size: 10px;
  font-family: "IBM Plex Mono", monospace;
  display: block;
}

.mini {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  min-height: 40px;
  align-items: center;
  border-top: 1px solid var(--line);
  font-size: 13px;
  cursor: pointer;
}
.mini .num { font-family: "IBM Plex Mono", monospace; font-size: 14px; white-space: nowrap; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.rows { width: 100%; border-collapse: collapse; }
.rows th {
  text-align: left;
  color: var(--muted);
  font-family: "IBM Plex Mono", monospace;
  font-size: 10px;
  font-weight: 400;
  padding: 0 8px 8px 0;
}
.rows td {
  padding: 10px 8px 10px 0;
  border-top: 1px solid var(--line);
  font-size: 14px;
  vertical-align: top;
}
.rows tr { cursor: pointer; }
.rows tr:hover td { color: #fff; }
.num { font-family: "IBM Plex Mono", monospace; font-size: 16px; font-weight: 500; text-align: right; }
.st { font-family: "IBM Plex Mono", monospace; text-align: right; width: 42px; }
.src { color: var(--muted); font-size: 11px; font-family: "IBM Plex Mono", monospace; }

.heat {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 6px;
}
.heat button {
  min-height: 72px;
  text-align: left;
  padding: 8px;
  border-color: transparent;
}
.heat button.GREEN { background: #10261d; }
.heat button.YELLOW { background: #2a2410; }
.heat button.ORANGE { background: #2a180e; }
.heat button.RED, .heat button.RED_ALERT { background: #2a1012; }
.heat button.UNKNOWN { background: #12151a; }
.heat .h-lab { display: block; font-size: 10px; color: var(--muted); line-height: 1.3; }
.heat .h-val { display: block; margin-top: 6px; font-size: 13px; }

.chain {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 12px;
}
.chip {
  border: 1px solid var(--line);
  padding: 8px 10px;
  display: inline-block;
}
.chip.GREEN { border-color: var(--green); color: var(--green); }
.chip.YELLOW { border-color: var(--yellow); color: var(--yellow); }
.chip.ORANGE { border-color: var(--orange); color: var(--orange); }
.chip.RED, .chip.RED_ALERT { border-color: var(--red); color: var(--red); }
.arrow { color: var(--muted); }

.chart-box { position: relative; height: 220px; width: 100%; }
.chart-box.sm { height: 170px; }
.chart-box.modal { height: min(42vh, 280px); }
.chart-box canvas { display: block; }

.sleeve {
  border: 1px solid var(--line);
  padding: 14px;
  background: #0c1015;
  min-width: 0;
}
.sleeve .stance {
  font-family: "IBM Plex Mono", monospace;
  font-size: clamp(16px, 2vw, 18px);
  font-weight: 600;
  margin: 4px 0 8px;
}
.sleeve p { margin: 0; font-size: 13px; line-height: 1.45; }
.sleeve .why { margin-top: 8px; color: var(--muted); font-family: "IBM Plex Mono", monospace; font-size: 11px; }

.GREEN { color: var(--green); }
.YELLOW { color: var(--yellow); }
.ORANGE { color: var(--orange); }
.RED, .RED_ALERT { color: var(--red); }
.UNKNOWN { color: var(--muted); }
.band.RED_ALERT { color: var(--alert); }

footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 4;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: var(--footer-h);
  padding: 10px max(var(--pad), var(--safe-r)) calc(10px + var(--safe-b)) max(var(--pad), var(--safe-l));
  background: #07080bf2;
  border-top: 1px solid var(--line);
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
  color: var(--muted);
}

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  width: min(760px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 16px;
  overflow: auto;
  transform-origin: center center;
}
dialog[open] {
  animation: dlg-pop-in 0.32s cubic-bezier(0.34, 1.45, 0.64, 1);
}
dialog.closing {
  animation: dlg-pop-out 0.18s ease-in forwards;
}
dialog::backdrop { background: #00000099; }
dialog[open]::backdrop {
  animation: dlg-fade-in 0.22s ease-out;
}
dialog.closing::backdrop {
  animation: dlg-fade-out 0.18s ease-in forwards;
}

@keyframes dlg-pop-in {
  from { opacity: 0; transform: scale(0.86) translateY(12px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
@keyframes dlg-pop-out {
  from { opacity: 1; transform: scale(1) translateY(0); }
  to { opacity: 0; transform: scale(0.92) translateY(8px); }
}
@keyframes dlg-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes dlg-fade-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  dialog[open], dialog.closing,
  dialog[open]::backdrop, dialog.closing::backdrop {
    animation: none;
  }
}
.dlg-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.dlg-head button { min-height: var(--tap); }
label { display: block; margin: 10px 0; font-size: 12px; color: var(--muted); }
input, select, textarea {
  width: 100%;
  margin-top: 4px;
  background: #0b0e12;
  color: var(--text);
  border: 1px solid var(--line);
  font-family: "IBM Plex Mono", monospace;
  font-size: 16px;
  padding: 10px;
}

@media (max-width: 960px) {
  :root { --pad: 16px; }
  .kpi-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
  .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .top {
    flex-direction: column;
    align-items: stretch;
    padding-top: calc(14px + var(--safe-t));
  }
  .top-meta {
    justify-content: space-between;
    flex-wrap: wrap;
  }
  .toast { max-width: none; flex: 1 1 100%; order: 3; }
  #refreshBtn { flex: 1; }
  h1 { font-size: 16px; }
  .sub { font-size: 10px; }
  .mark { font-size: 16px; padding: 7px 8px; }
  .chart-box { height: 200px; }
  .chart-box.sm { height: 160px; }
  .heat { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
}

@media (max-width: 640px) {
  .rows thead { display: none; }
  .rows, .rows tbody { display: block; width: 100%; }
  .rows tr {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas: "lab st" "val val";
    gap: 2px 12px;
    padding: 12px 0;
    border-top: 1px solid var(--line);
  }
  .rows td {
    padding: 0;
    border: none;
    text-align: left;
  }
  .rows td:first-child { grid-area: lab; }
  .rows td.num { grid-area: val; text-align: left; font-size: 18px; }
  .rows td.st { grid-area: st; width: auto; }
}

@media (max-width: 420px) {
  .kpi-row { grid-template-columns: 1fr; }
  .heat { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 1280px) {
  main { margin: 0 auto; }
}
