:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --border: #e2e0dc;
  --text: #22201d;
  --muted: #6f6a63;
  --accent: #2f6f4f;
  --accent-text: #ffffff;
  --warn: #8a6100;
  --warn-bg: #fdf5e2;
  --error: #a32c22;
  --error-bg: #fcf0ee;
  --ok: #2f6f4f;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #1e2124;
    --border: #32363b;
    --text: #e6e3de;
    --muted: #9a948c;
    --accent: #5fb98a;
    --accent-text: #10231a;
    --warn: #e0b04a;
    --warn-bg: #2b2517;
    --error: #e88b80;
    --error-bg: #2d1c1a;
    --ok: #5fb98a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

body.centered {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
}

a { color: var(--accent); }

code, .mono { font-family: var(--mono); font-size: 0.92em; }

/* ---- top bar ---- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.01em;
  font-size: 17px;
}

.modes { display: flex; gap: 8px; flex-wrap: wrap; }

.mode {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 14px 7px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
  transition: border-color 0.12s, background 0.12s;
}
.mode:hover { border-color: var(--accent); }
.mode.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.mode.active .mode-text small { color: var(--accent-text); opacity: 0.8; }

.mode-icon { font-size: 15px; line-height: 1; opacity: 0.85; }
.mode-text { display: flex; flex-direction: column; line-height: 1.2; }
.mode-text b { font-size: 13.5px; font-weight: 600; }
.mode-text small { font-size: 11px; color: var(--muted); }

.mode.secondary { padding: 7px 12px; }
.mode.secondary:not(.active) { background: transparent; border-color: transparent; }
.mode.secondary:not(.active):hover { border-color: var(--border); }

.account { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.who { color: var(--muted); font-size: 13px; }

.usage { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; }
.usage-cell { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.usage-cell b { color: var(--text); font-weight: 600; }
.usage-cell .money { color: var(--text); font-variant-numeric: tabular-nums; }
.usage-cell.live .money { color: var(--accent); font-weight: 600; }
.usage-cell.prices.stale { color: var(--warn); }
.usage-cell.prices.critical, .usage-cell.prices.unknown { color: var(--error); }

.meter {
  display: inline-block;
  width: 70px; height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.meter i { display: block; height: 100%; background: var(--ok); }
.meter.warn i { background: var(--warn); }
.meter.over i { background: var(--error); }

.badge {
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.badge-dry { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn); }

/* ---- layout ---- */

.page { max-width: 940px; margin: 0 auto; padding: 28px 20px 80px; }
h1 { font-size: 26px; margin: 0 0 6px; letter-spacing: -0.02em; }
h2 { font-size: 17px; margin: 0 0 12px; }
.lede { color: var(--muted); margin: 0 0 22px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 22px;
}

.auth-card { width: min(400px, 100%); }

.row { display: flex; gap: 16px; flex-wrap: wrap; }
.row .field { flex: 1 1 200px; }

.field { display: block; margin-bottom: 16px; }
.label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
.hint { color: var(--muted); font-size: 12px; display: block; margin-top: 4px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--muted); margin-bottom: 14px; }

input[type=text], input[type=email], input[type=password], input[type=number],
select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
textarea { font-family: var(--mono); font-size: 13px; resize: vertical; }
select:disabled, option:disabled { color: var(--muted); }

.actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-top: 6px; }

.btn {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.wide { width: 100%; }
.link { background: none; border: none; color: var(--muted); cursor: pointer; font: inherit; padding: 0; }
.link:hover { color: var(--text); }
.inline { display: inline; }

/* ---- alerts ---- */

.alert { padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; font-size: 14px; }
.alert.error { background: var(--error-bg); color: var(--error); border: 1px solid var(--error); }
.alert.warn { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn); }

/* ---- estimate ---- */

.estimate-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 16px;
  margin-bottom: 14px;
}
.estimate-grid b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.estimate-grid small { color: var(--muted); font-size: 12px; }
.notes { color: var(--muted); font-size: 13px; padding-left: 18px; margin: 0 0 14px; }

/* ---- job page ---- */

.job-head { display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; align-items: flex-start; }
.job-actions { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }

.progress { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin-bottom: 6px; }
.progress i { display: block; height: 100%; background: var(--accent); transition: width 0.3s; }
.progress-wrap { margin-bottom: 22px; }
.progress-meta { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.progress-meta span:first-child { color: var(--text); font-variant-numeric: tabular-nums; }

.chunk-grid { display: flex; flex-wrap: wrap; gap: 3px; }
.chunk {
  width: 26px; height: 22px;
  display: grid; place-items: center;
  border-radius: 3px;
  font-size: 10px;
  font-family: var(--mono);
  background: var(--border);
  color: var(--muted);
}
.chunk-done { background: var(--ok); color: var(--accent-text); }
.chunk-running { background: var(--accent); color: var(--accent-text); animation: pulse 1.2s ease-in-out infinite; }
.chunk-retried { background: var(--warn); color: #1a1508; }
.chunk-failed { background: var(--error); color: #fff; }
@keyframes pulse { 50% { opacity: 0.45; } }

.status { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 10px; background: var(--border); }
.status-done, .status-ok { background: var(--ok); color: var(--accent-text); }
.status-running, .status-queued { background: var(--accent); color: var(--accent-text); }
.status-failed, .status-budget_stopped { background: var(--error); color: #fff; }
.status-cancelled, .status-interrupted, .status-draft { background: var(--warn); color: #1a1508; }

/* ---- table ---- */

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

/* ---- log ---- */

.log { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow-y: auto; font-family: var(--mono); font-size: 12px; }
.log li { padding: 3px 0; border-bottom: 1px solid var(--border); color: var(--muted); }
.log .log-warning { color: var(--warn); }
.log .log-error { color: var(--error); }

/* ---- rules strip on the tagging page ---- */

.rules {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.rule {
  border-left: 3px solid var(--accent);
  padding: 2px 0 2px 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}
.rule b { display: block; color: var(--text); font-size: 13.5px; margin-bottom: 2px; }

/* ---- tag set display ---- */

.tagset-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.tagset-head h2 { margin-bottom: 2px; }
.tagset-head h2 a { text-decoration: none; }
.tagset-counts { display: flex; gap: 14px; align-items: flex-start; }
.count { font-size: 12px; color: var(--muted); }
.count b { display: block; font-size: 18px; color: var(--text); font-variant-numeric: tabular-nums; }

.tag-row { display: flex; gap: 12px; margin: 10px 0; align-items: baseline; }
.tag-row-label {
  flex: 0 0 96px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.tag-row-label small { font-weight: 400; text-transform: none; letter-spacing: 0; }
.tag-row-tags { display: flex; flex-wrap: wrap; gap: 5px; }

.tagset-preview { margin: 4px 0 18px; }
.tagset-preview-inner {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--bg);
}
.tagset-preview-head {
  display: flex;
  gap: 10px;
  align-items: baseline;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.pill-yours, .pill-locked {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 9px;
  font-weight: 600;
}
.pill-yours { background: var(--accent); color: var(--accent-text); }
.pill-locked { background: var(--border); color: var(--muted); }

.editor { font-family: var(--mono); font-size: 12.5px; line-height: 1.5; }
.guidance {
  font-family: var(--mono);
  font-size: 12.5px;
  white-space: pre-wrap;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  margin: 0;
  overflow-x: auto;
}

/* ---- tag pills in the review pane ---- */

.tag {
  display: inline-block;
  padding: 0 6px;
  margin: 0 3px;
  border-radius: 10px;
  font-size: 11px;
  font-family: var(--mono);
  vertical-align: middle;
}
.tag-tone { background: #2f6f4f22; color: var(--accent); border: 1px solid var(--accent); }
.tag-motion { background: #2b6cb022; color: #3182ce; border: 1px solid #3182ce; }
.tag-expression { background: #6b46c122; color: #805ad5; border: 1px solid #805ad5; }
.tag-func { background: var(--border); color: var(--muted); border: 1px solid var(--muted); }

/* ---- failure explanation ---- */

.explanation b { display: block; margin-bottom: 4px; font-size: 14.5px; }
.explanation p { margin: 4px 0; }
.explanation-action { font-weight: 500; }
.explanation details { margin-top: 8px; }
.explanation summary { cursor: pointer; font-size: 12px; opacity: 0.75; }
.explanation pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 11.5px;
  margin: 6px 0 0;
  opacity: 0.8;
}


/* ---- mobile -----------------------------------------------------------
   Desktop stays the primary target and is deliberately untouched: every rule
   below sits inside a media query. The aim on a phone is not a redesign, only
   that nothing overflows sideways and every control stays reachable.
*/

/* A table is the one thing here that cannot reflow honestly — squeezing
   columns loses the numbers. Let it scroll inside its own box instead of
   pushing the whole page sideways. */
.table-wrap { overflow-x: auto; }

@media (max-width: 760px) {
  .page { padding: 18px 14px 60px; }

  /* Once the bar wraps to three lines, sticky eats a third of a phone
     screen. It scrolls away instead. */
  .topbar { position: static; gap: 10px; padding: 9px 14px; }
  .brand { font-size: 16px; }

  /* The two modes are the product; they get the full width and split it
     evenly rather than shrinking to fit beside everything else. */
  .modes { width: 100%; gap: 6px; }
  .mode { flex: 1; justify-content: center; padding: 9px 8px; }
  .mode-text { align-items: center; }
  .mode-text small { display: none; }
  .mode.secondary { flex: 0 0 auto; }

  .account { margin-left: 0; width: 100%; justify-content: space-between; }
  .usage { width: 100%; gap: 12px; font-size: 12px; }

  /* iOS zooms the viewport when a field under 16px takes focus, which leaves
     the user scrolled sideways on a page that otherwise fits. */
  input[type=text], input[type=email], input[type=password], input[type=number],
  select, textarea { font-size: 16px; }

  /* ~44px tall, the usual minimum for a reliable tap. */
  .btn { padding: 11px 16px; }
  .actions { gap: 10px; }

  /* The label column is a fixed 96px; stack it rather than crushing the tags. */
  .tag-row { flex-direction: column; gap: 4px; }
  .tag-row-label { flex: none; }

  .job-head { flex-direction: column; gap: 12px; }
  .job-actions { align-items: flex-start; width: 100%; }
  .tagset-head { flex-direction: column; gap: 10px; }

  .estimate-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .estimate-grid b { font-size: 18px; }

  h1 { font-size: 22px; }
  .card { padding: 16px; }
}

@media (max-width: 430px) {
  .mode-icon { display: none; }
  .mode-text b { font-size: 13px; }
  .table { font-size: 13px; }
  .table th, .table td { padding: 6px 8px; }
  .chunk { width: 23px; height: 20px; }
  .usage { gap: 10px; }
  .usage-cell .meter { display: none; }
}
