/* =====================================================================
 * Emers 2027 Targets
 *
 * Two themes, one set of variables. Everything below the :root blocks is
 * theme agnostic, so adding a third theme later means adding one block.
 *
 * Theme is chosen in this order:
 *   1. what the user last picked, remembered in localStorage
 *   2. the operating system preference
 *   3. light
 * ===================================================================== */

:root,
:root[data-theme="light"] {
  --accent:      #1f6feb;   /* primary blue */
  --accent-2:    #175bc9;   /* hover        */
  --accent-soft: #e8f0fe;   /* tinted fill  */

  --ink:   #14171a;
  --ink-2: #4a5259;
  --ink-3: #79838c;

  --line:   #e3e7ea;
  --line-2: #f1f4f6;

  --bg:     #f6f8fa;
  --panel:  #ffffff;
  --raised: #fafbfc;   /* table headers, footers */
  --hover:  #fcfcfd;

  --ok:        #157f4b;
  --ok-bg:     #e8f5ee;
  --warn:      #8a5a00;
  --warn-bg:   #fdf3e0;
  --danger:    #b3261e;
  --danger-bg: #fdecea;
  --danger-line: #f0c9c6;

  --dirty-bg:   #eef4ff;
  --dirty-line: #b6cffb;

  --shadow: 0 1px 2px rgba(20,23,26,.06), 0 4px 14px rgba(20,23,26,.05);
  --focus:  rgba(31,111,235,.18);
  --topbar: rgba(255,255,255,.92);

  color-scheme: light;
}

:root[data-theme="dark"] {
  --accent:      #4c8dff;
  --accent-2:    #6ba1ff;
  --accent-soft: #17273f;

  --ink:   #e6edf3;
  --ink-2: #a9b4bf;
  --ink-3: #7b8794;

  --line:   #2a313b;
  --line-2: #1d232b;

  --bg:     #0d1117;
  --panel:  #161b22;
  --raised: #1b2129;
  --hover:  #1c222b;

  --ok:        #4ec98a;
  --ok-bg:     #12301f;
  --warn:      #e0a33a;
  --warn-bg:   #33260d;
  --danger:    #ff7b72;
  --danger-bg: #3a1a17;
  --danger-line: #5c2b26;

  --dirty-bg:   #16243a;
  --dirty-line: #2f4d7d;

  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  --focus:  rgba(76,141,255,.28);
  --topbar: rgba(22,27,34,.9);

  color-scheme: dark;
}

/* Follow the OS until the user picks a side. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --accent: #4c8dff; --accent-2: #6ba1ff; --accent-soft: #17273f;
    --ink: #e6edf3; --ink-2: #a9b4bf; --ink-3: #7b8794;
    --line: #2a313b; --line-2: #1d232b;
    --bg: #0d1117; --panel: #161b22; --raised: #1b2129; --hover: #1c222b;
    --ok: #4ec98a; --ok-bg: #12301f;
    --warn: #e0a33a; --warn-bg: #33260d;
    --danger: #ff7b72; --danger-bg: #3a1a17; --danger-line: #5c2b26;
    --dirty-bg: #16243a; --dirty-line: #2f4d7d;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
    --focus: rgba(76,141,255,.28);
    --topbar: rgba(22,27,34,.9);
    color-scheme: dark;
  }
}

:root {
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
        "PingFang TC", "Microsoft JhengHei", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  transition: background .15s, color .15s;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 14px; }

.hidden { display: none !important; }
.muted { color: var(--ink-3); }
.small { font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; font-family: var(--mono); }
.right { text-align: right; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }

/* ---------- theme toggle ---------- */
.themeBtn {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink-2);
  width: 30px; height: 30px; padding: 0; border-radius: 8px;
  display: grid; place-items: center; cursor: pointer; font-size: 14px; line-height: 1;
}
.themeBtn:hover { background: var(--line-2); color: var(--ink); }

/* ---------- login ---------- */
#login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow); padding: 30px 28px;
  position: relative;
}
.login-card .mark {
  width: 38px; height: 38px; border-radius: 9px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 15px; margin-bottom: 16px;
}
.login-card .themeBtn { position: absolute; top: 22px; right: 22px; }

/* ---------- shell ---------- */
header.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--topbar);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
  padding: 0 20px;
}
.topbar-inner { display: flex; align-items: center; gap: 14px; height: 56px; max-width: 1500px; margin: 0 auto; }
.brandmark { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: -.01em; }
.brandmark .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
nav.tabs { display: flex; gap: 2px; margin-left: 10px; }
nav.tabs button {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 7px;
  font: inherit; font-size: 14px; color: var(--ink-2); cursor: pointer;
}
nav.tabs button:hover { background: var(--line-2); color: var(--ink); }
nav.tabs button.active { background: var(--accent); color: #fff; }

main { max-width: 1500px; margin: 0 auto; padding: 22px 20px 80px; }

/* ---------- countdown ---------- */
.countdown {
  display: flex; align-items: center; gap: 10px;
  padding: 5px 12px; border-radius: 999px;
  background: var(--ok-bg); color: var(--ok);
  font-size: 13px; font-weight: 600; white-space: nowrap;
}
.countdown.soon   { background: var(--warn-bg);   color: var(--warn); }
.countdown.urgent { background: var(--danger-bg); color: var(--danger); }
.countdown .num { font-weight: 700; }

/* ---------- panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 18px; overflow: hidden;
}
.panel > header {
  padding: 14px 16px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.panel > .body { padding: 16px; }

/* ---------- buttons ---------- */
button, .btn {
  font: inherit; font-size: 14px;
  border: 1px solid var(--line);
  background: var(--panel); color: var(--ink);
  padding: 7px 13px; border-radius: 8px; cursor: pointer;
  transition: background .12s, border-color .12s;
}
button:hover:not(:disabled) { background: var(--line-2); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary, button.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled), button.accent:hover:not(:disabled) {
  background: var(--accent-2); border-color: var(--accent-2);
}
button.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
button.ghost:hover:not(:disabled) { background: var(--line-2); color: var(--ink); }
button.tiny { padding: 4px 9px; font-size: 12.5px; }
button.danger { color: var(--danger); border-color: var(--danger-line); }

/* ---------- forms ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
input, select, textarea {
  font: inherit; width: 100%;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; background: var(--panel); color: var(--ink);
}
input::placeholder, textarea::placeholder { color: var(--ink-3); }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus);
}
textarea { min-height: 78px; resize: vertical; }
input.cell {
  border: 1px solid transparent; background: transparent;
  text-align: right; padding: 6px 8px; border-radius: 6px;
  font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 13.5px;
}
input.cell:hover { border-color: var(--line); }
input.cell:focus { border-color: var(--accent); background: var(--panel); }
input.cell.dirty { background: var(--dirty-bg); border-color: var(--dirty-line); }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line-2); white-space: nowrap; }
th {
  font-size: 11.5px; font-weight: 650; text-transform: uppercase;
  letter-spacing: .05em; color: var(--ink-3);
  background: var(--raised); position: sticky; top: 0; z-index: 2;
  border-bottom: 1px solid var(--line);
}
tbody tr:hover { background: var(--hover); }
tfoot td { font-weight: 700; background: var(--raised); border-top: 1px solid var(--line); border-bottom: 0; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }

/* ---------- badges ---------- */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 650;
  background: var(--line-2); color: var(--ink-2);
}
.badge.ok    { background: var(--ok-bg);     color: var(--ok); }
.badge.draft { background: var(--warn-bg);   color: var(--warn); }
.badge.warn  { background: var(--warn-bg);   color: var(--warn); }
.badge.bad   { background: var(--danger-bg); color: var(--danger); }
.badge.none  { background: var(--line-2);    color: var(--ink-3); }
.badge.tbc   { background: var(--accent-soft); color: var(--accent); }

/* ---------- zero flagging ----------
 * A single channel at zero is often deliberate strategy, so it gets a
 * soft amber. A market whose entire target is zero is far more likely to
 * be an oversight, so the whole row goes red. */
td.zero {
  background: var(--warn-bg);
  color: var(--warn);
  font-weight: 650;
}
tr.zero-row > td { background: var(--danger-bg); }
tr.zero-row > td.zero { background: transparent; color: var(--danger); }
tr.zero-row:hover > td { background: var(--danger-bg); filter: brightness(1.06); }
#grid th:first-child, #grid td:first-child { }  /* sticky rule lives in the media query */

/* ---------- brand cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(268px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px; cursor: pointer;
  box-shadow: var(--shadow); transition: transform .12s, border-color .12s;
}
.card:hover { transform: translateY(-1px); border-color: var(--accent); }
.card .tile, .tile {
  width: 34px; height: 34px; border-radius: 8px; color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 12px;
}
.bar { height: 5px; border-radius: 3px; background: var(--line-2); overflow: hidden; margin-top: 11px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .3s; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 15px; }
.stat .k { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); font-weight: 650; }
.stat .v { font-size: 23px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }

/* ---------- misc ---------- */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--accent); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13.5px; box-shadow: 0 6px 24px rgba(0,0,0,.28); z-index: 100;
  max-width: 90vw; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.err { background: var(--danger); color: #fff; }
:root[data-theme="dark"] .toast.err { color: #2b0f0d; }
.err-msg { color: var(--danger); font-size: 13px; margin: 8px 0 0; min-height: 18px; }
.empty { padding: 40px 20px; text-align: center; color: var(--ink-3); }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px;
  border-radius: 999px; background: var(--accent-soft); color: var(--ink); font-size: 12.5px;
}
.chip button { border: 0; background: none; padding: 0 0 0 2px; color: var(--ink-3); font-size: 15px; line-height: 1; cursor: pointer; }
.chip button:hover { color: var(--danger); background: none; }
hr.sep { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar select, .toolbar input { width: auto; min-width: 130px; }

/* ---------- modal ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 150;
  background: rgba(8, 11, 15, .55);
  backdrop-filter: blur(2px);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto;
}
.modal {
  width: 100%; max-width: 900px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: 0 18px 50px rgba(0,0,0,.4);
  overflow: hidden; animation: modalIn .16s ease-out;
}
@keyframes modalIn { from { opacity: 0; transform: translateY(-6px); } }
.modal > header {
  padding: 15px 18px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  position: sticky; top: 0; background: var(--panel); z-index: 2;
}
.modal-body { padding: 0; }
.modal-body .sec { padding: 14px 18px; border-bottom: 1px solid var(--line-2); }
.modal-body .sec:last-child { border-bottom: 0; }
.modal-body h3 { margin-bottom: 10px; color: var(--ink-2); }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--accent-soft); }

@media (max-width: 560px) {
  .modal-back { padding: 12px 8px; }
}

/* ---------- floating tooltip ---------- */
[data-tip] { cursor: help; text-decoration: underline dotted var(--ink-3); text-underline-offset: 3px; }
.tip {
  position: fixed; z-index: 200; pointer-events: none;
  max-width: min(300px, 88vw);
  background: var(--ink); color: var(--bg);
  padding: 9px 12px; border-radius: 8px;
  font-size: 12.5px; line-height: 1.55; white-space: pre-line;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 6px 22px rgba(0,0,0,.28);
  opacity: 0; transition: opacity .12s;
}
.tip.show { opacity: 1; }
:root[data-theme="dark"] .tip { background: #2b333d; color: var(--ink); border: 1px solid var(--line); }

/* =====================================================================
 * RESPONSIVE
 * The entry grid is genuinely wide, so on small screens it scrolls
 * sideways with the market name frozen in place. Everything else
 * collapses to a single column.
 * ===================================================================== */

@media (max-width: 900px) {
  main { padding: 16px 12px 60px; }
  header.topbar { padding: 0 12px; }

  .topbar-inner {
    height: auto; min-height: 54px; flex-wrap: wrap;
    row-gap: 8px; padding: 9px 0;
  }
  .topbar-inner .grow { flex-basis: 100%; order: 5; height: 0; }
  nav.tabs { margin-left: 0; order: 10; width: 100%; overflow-x: auto; }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs button { white-space: nowrap; }
  .countdown { order: 6; font-size: 12px; padding: 4px 10px; }

  .panel > header { flex-wrap: wrap; row-gap: 10px; }
  .panel > header .row { flex-wrap: wrap; }
  .stat .v { font-size: 20px; }
  h1 { font-size: 19px; }

  .toolbar { width: 100%; }
  .toolbar select, .toolbar input { flex: 1 1 140px; min-width: 0; }

  /* Freeze the market name so a horizontally scrolled grid stays legible. */
  #grid th:first-child, #grid td:first-child {
    position: sticky; left: 0; z-index: 3;
    background: var(--panel);
    box-shadow: 1px 0 0 var(--line);
  }
  #grid th:first-child { background: var(--raised); z-index: 4; }
  #grid tbody tr:hover td:first-child { background: var(--hover); }
  #grid input.cell { min-width: 74px; }

  .tablewrap { -webkit-overflow-scrolling: touch; }
}

@media (max-width: 560px) {
  .cards { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr 1fr; }

  /* Reclaim a whole top bar row. "Emers" alone is enough branding. */
  .brandmark > .muted { display: none; }

  /* Region is inferable from the market name and costs width that the
     numbers need more. Still exported in the CSV. */
  #grid th:nth-child(2), #grid td:nth-child(2) { display: none; }

  .badge { white-space: nowrap; }
  .login-card { padding: 26px 20px; }
  main { padding: 14px 10px 60px; }
  th, td { padding: 7px 8px; }
  .panel > header { padding: 12px; }
  .panel > .body { padding: 12px; }

  /* Full width buttons in panel headers beat three cramped ones in a row. */
  .panel > header .row { width: 100%; }
  .panel > header .row > button { flex: 1; }

  .toast { left: 10px; right: 10px; transform: none; max-width: none; }
  .toast.show { transform: translateY(-4px); }
}

/* Touch targets. A 24px checkbox is a miss on a phone. */
@media (pointer: coarse) {
  button, .btn { padding: 9px 14px; }
  button.tiny { padding: 7px 11px; }
  input, select, textarea { padding: 10px; font-size: 16px; } /* 16px stops iOS zooming on focus */
  input.cell { padding: 9px 8px; font-size: 15px; }
  [data-tip] { cursor: pointer; }
}
