:root {
  --bg: #ebe6dc;
  --bg-2: #f4efe6;
  --ink: #171c19;
  --ink-soft: #3d4741;
  --muted: #6d776f;
  --line: rgba(23, 28, 25, 0.12);
  --accent: #0d6b54;
  --accent-2: #148f6f;
  --danger: #9b2f2f;
  --ok: #0d6b54;
  --warn: #9a6b14;
  --radius: 14px;
  --font-display: "Syne", system-ui, sans-serif;
  --font-body: "Figtree", system-ui, sans-serif;
  --shadow-soft: 0 18px 40px rgba(23, 28, 25, 0.08);
  --tg-safe-top: env(safe-area-inset-top, 0px);
  --tg-safe-bot: env(safe-area-inset-bottom, 0px);
  --progress: #0d6b54;
  --surface: rgba(255, 252, 247, 0.78);
  --surface-solid: #fffcf7;
  --shell-max: 1120px;
  --pad: 16px;
  --dock-h: 0px;
}

html[data-theme="dark"] {
  --bg: #121614;
  --bg-2: #1a201c;
  --ink: #ebe8e1;
  --ink-soft: #c5c8c2;
  --muted: #8a938c;
  --line: rgba(235, 232, 225, 0.12);
  --accent: #3dba95;
  --accent-2: #52d0a8;
  --danger: #e07070;
  --ok: #3dba95;
  --warn: #d4a84b;
  --shadow-soft: 0 18px 40px rgba(0, 0, 0, 0.35);
  --progress: #3dba95;
  --surface: rgba(26, 32, 28, 0.82);
  --surface-solid: #1a201c;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  position: relative;
  overflow-x: hidden;
  min-height: 100dvh;
}

.noise {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.045;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}

.wash {
  pointer-events: none;
  position: fixed;
  inset: -20% -10% auto;
  height: 55vh;
  background:
    radial-gradient(ellipse 70% 55% at 15% 20%, rgba(13, 107, 84, 0.16), transparent 60%),
    radial-gradient(ellipse 55% 45% at 90% 0%, rgba(154, 107, 20, 0.12), transparent 55%);
  z-index: 0;
  animation: drift 14s ease-in-out infinite alternate;
}

html[data-theme="dark"] .wash {
  background:
    radial-gradient(ellipse 70% 55% at 15% 20%, rgba(61, 186, 149, 0.14), transparent 60%),
    radial-gradient(ellipse 55% 45% at 90% 0%, rgba(212, 168, 75, 0.1), transparent 55%);
}

@keyframes drift {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to { transform: translate3d(-2%, 3%, 0) scale(1.05); }
}

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes blink {
  0%, 45% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(13, 107, 84, 0.4); }
  70% { box-shadow: 0 0 0 10px rgba(13, 107, 84, 0); }
  100% { box-shadow: 0 0 0 0 rgba(13, 107, 84, 0); }
}

/* ── Gate ── */
.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(24px, var(--tg-safe-top)) 20px max(24px, var(--tg-safe-bot));
  background:
    radial-gradient(ellipse 70% 55% at 20% 20%, rgba(13, 107, 84, 0.14), transparent 60%),
    var(--bg);
}

.gate[hidden] { display: none !important; }

.gate-card {
  width: min(100%, 400px);
  padding: 28px 22px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  backdrop-filter: blur(12px);
  animation: rise 0.5s cubic-bezier(.2,.8,.2,1) both;
}

.gate-brand {
  margin: 0 0 6px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.6rem, 6vw, 1.9rem);
  letter-spacing: -0.04em;
}

.gate-sub {
  margin: 0 0 18px;
  color: var(--ink-soft);
  font-size: 0.95rem;
}

.gate-err {
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 0.88rem;
  font-weight: 600;
}

/* ── Shell ── */
.shell {
  position: relative;
  z-index: 1;
  width: min(100%, var(--shell-max));
  margin: 0 auto;
  padding:
    calc(12px + var(--tg-safe-top))
    var(--pad)
    calc(24px + var(--tg-safe-bot) + var(--dock-h));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

#app[hidden] { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  animation: rise 0.55s cubic-bezier(.2,.8,.2,1) both;
  position: sticky;
  top: 0;
  z-index: 8;
  padding: 8px 0 10px;
  background: linear-gradient(180deg, var(--bg) 70%, transparent);
  backdrop-filter: blur(8px);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.brand {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.45rem, 5.5vw, 2.1rem);
  letter-spacing: -0.045em;
  line-height: 1.1;
  min-height: 1.15em;
  display: inline-flex;
  align-items: center;
  min-width: 0;
}

.brand .caret {
  display: inline-block;
  width: 0.08em;
  height: 0.92em;
  margin-left: 0.08em;
  background: var(--accent);
  border-radius: 1px;
  animation: blink 0.85s steps(1) infinite;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-size: 1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.2s ease, transform 0.15s ease;
}

.icon-btn:active { transform: scale(0.96); }

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: lowercase;
  color: var(--ink-soft);
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}

.status[data-state="running"] .status-dot,
.status[data-state="checking"] .status-dot {
  background: var(--accent);
  animation: pulse 1.6s ease-out infinite;
}
.status[data-state="running"],
.status[data-state="checking"] { color: var(--accent); }
.status[data-state="stopping"] .status-dot { background: var(--warn); }
.status[data-state="stopping"] { color: var(--warn); }

/* ── Tabs ── */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  position: sticky;
  top: 62px;
  z-index: 7;
  animation: rise 0.6s 0.05s cubic-bezier(.2,.8,.2,1) both;
}

.tabs::-webkit-scrollbar { display: none; }

.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.84rem;
  padding: 11px 14px;
  border-radius: 12px;
  cursor: pointer;
  white-space: nowrap;
  flex: 1 0 auto;
  min-height: 42px;
  transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
}

.tab.is-active {
  background: var(--ink);
  color: var(--bg-2);
}

.tab:active { transform: scale(0.97); }

.content {
  flex: 1;
  min-width: 0;
}

.panel { display: none; animation: rise 0.4s cubic-bezier(.2,.8,.2,1) both; }
.panel.is-active { display: block; }

.panel-lead {
  margin: 0 0 12px;
  color: var(--ink-soft);
  font-size: 0.92rem;
}

/* ── Run layout ── */
.run-grid {
  display: grid;
  gap: 18px;
}

.meter {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.meter-item {
  padding: 12px 8px;
  border-top: 2px solid var(--ink);
  background: linear-gradient(180deg, rgba(255,255,255,0.28), transparent);
  min-width: 0;
}

html[data-theme="dark"] .meter-item {
  background: linear-gradient(180deg, rgba(255,255,255,0.04), transparent);
}

.meter-val {
  display: block;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.1rem, 3.5vw, 1.4rem);
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.meter-label {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.progress-wrap {
  margin: 0 0 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(13, 107, 84, 0.06);
}

html[data-theme="dark"] .progress-wrap {
  background: rgba(61, 186, 149, 0.08);
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 8px;
}

.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.progress-bar i {
  display: block;
  height: 100%;
  background: var(--progress);
  border-radius: inherit;
  transition: width 0.35s ease;
}

.progress-pct {
  margin-top: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--accent);
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 4px;
}

.settings-span { grid-column: 1 / -1; }

.toggles {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin: 14px 0 4px;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 0.9rem;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
}

.check input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.hint {
  display: block;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.76rem;
}

/* ── Fields ── */
.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.field > span {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}

.field small {
  color: var(--muted);
  font-size: 0.78rem;
}

.field-grow {
  flex: 1;
  margin-bottom: 0;
}

.field-grow textarea {
  min-height: 42vh;
}

textarea,
input[type="number"],
input[type="text"],
input[type="password"],
input[type="datetime-local"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-solid);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px; /* iOS zoom guard */
  padding: 12px 14px;
  outline: none;
  resize: vertical;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  min-height: 46px;
}

textarea:focus,
input:focus {
  border-color: rgba(13, 107, 84, 0.55);
  box-shadow: 0 0 0 3px rgba(13, 107, 84, 0.12);
}

html[data-theme="dark"] textarea:focus,
html[data-theme="dark"] input:focus {
  border-color: rgba(61, 186, 149, 0.55);
  box-shadow: 0 0 0 3px rgba(61, 186, 149, 0.14);
}

/* ── Buttons / toolbars ── */
.toolbar,
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.actions-desktop { display: none; }

.btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.92rem;
  padding: 13px 16px;
  min-height: 46px;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease, opacity 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn-block { width: 100%; }

.btn-primary {
  background: var(--accent);
  color: #f4efe6;
  flex: 1 1 auto;
  min-width: 110px;
}
.btn-primary:hover { background: var(--accent-2); }

.btn-ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  flex: 0 1 auto;
}

.btn-accent {
  background: transparent;
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  flex: 1 1 auto;
}

.file-btn {
  display: inline-flex;
  flex: 1 1 auto;
  min-width: 120px;
}

/* ── Mobile dock ── */
.dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: flex;
  gap: 8px;
  padding: 10px var(--pad) calc(10px + var(--tg-safe-bot));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(14px);
  box-shadow: 0 -8px 30px rgba(23, 28, 25, 0.06);
}

.dock .btn { flex: 1; min-width: 0; padding-inline: 10px; }
.dock .btn-ghost { flex: 0.7; }

.shell:has(#panel-run:not(.is-active)) .dock,
.shell:not(:has(#panel-run.is-active)) .dock {
  display: none;
}

/* fallback if :has unsupported — JS toggles .dock-hidden */
.dock.dock-hidden { display: none !important; }

.shell:has(#panel-run.is-active):not(:has(.dock.dock-hidden)) {
  --dock-h: 72px;
}

/* ── Log ── */
.log {
  border-top: 2px solid var(--ink);
  max-height: calc(100dvh - 220px - var(--dock-h));
  overflow: auto;
  padding-top: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.55;
  -webkit-overflow-scrolling: touch;
}

.log-row {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
  color: var(--ink-soft);
}

.log-row time { color: var(--muted); }
.log-user {
  font-style: normal;
  color: var(--accent);
  font-weight: 600;
  margin-right: 4px;
}

/* ── Admin / notes ── */
.admin-grid {
  display: grid;
  gap: 0;
}

.notes-list {
  display: grid;
  gap: 10px;
  margin-top: 8px;
}

.note-card {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}

.note-card h3 {
  margin: 0 0 6px;
  font-family: var(--font-display);
  font-size: 1.05rem;
}

.note-meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 4px;
}

.note-actions { margin-top: 10px; }

/* ── Toast ── */
.toast {
  position: fixed;
  left: var(--pad);
  right: var(--pad);
  bottom: calc(18px + var(--tg-safe-bot) + var(--dock-h));
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg-2);
  font-weight: 600;
  box-shadow: var(--shadow-soft);
  z-index: 30;
  animation: rise 0.25s ease both;
  max-width: 480px;
  margin: 0 auto;
}

.toast[hidden] { display: none !important; }

/* ── Phone tweaks ── */
@media (max-width: 520px) {
  :root { --pad: 14px; }

  .meter { grid-template-columns: repeat(3, 1fr); }
  .meter-item:nth-child(4),
  .meter-item:nth-child(5) { grid-column: span 1; }

  .brand { font-size: 1.35rem; }
  .status span:not(.status-dot) {
    max-width: 78px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .settings-grid { grid-template-columns: 1fr 1fr; }
  .tab { padding: 11px 12px; font-size: 0.8rem; }
}

@media (max-width: 380px) {
  .meter { grid-template-columns: repeat(2, 1fr); }
  .dock .btn { font-size: 0.82rem; padding-inline: 8px; }
}

/* ── Tablet+ ── */
@media (min-width: 720px) {
  :root {
    --pad: 24px;
    --dock-h: 0px;
  }

  .shell {
    padding-top: calc(22px + var(--tg-safe-top));
    padding-bottom: calc(36px + var(--tg-safe-bot));
  }

  .dock { display: none !important; }
  .actions-desktop { display: flex; }

  .topbar { position: static; background: transparent; backdrop-filter: none; padding: 0 0 4px; }
  .tabs {
    position: static;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
  }

  .run-grid {
    grid-template-columns: minmax(240px, 320px) 1fr;
    gap: 28px;
    align-items: start;
  }

  .run-stats {
    position: sticky;
    top: 18px;
  }

  .meter { grid-template-columns: repeat(2, 1fr); }
  .meter-item:last-child { grid-column: 1 / -1; }

  .toggles { grid-template-columns: repeat(3, 1fr); }

  .settings-grid {
    grid-template-columns: 1fr 1fr;
  }

  .field-grow textarea { min-height: 58vh; }

  .log { max-height: calc(100dvh - 200px); }

  .admin-grid {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .toast {
    left: auto;
    right: 24px;
    bottom: 24px;
    width: min(360px, calc(100vw - 48px));
  }
}

/* ── Desktop wide ── */
@media (min-width: 980px) {
  .run-grid {
    grid-template-columns: 340px 1fr;
    gap: 36px;
  }

  .meter { grid-template-columns: repeat(3, 1fr); }
  .meter-item:last-child { grid-column: auto; }

  .toggles { grid-template-columns: repeat(3, 1fr); }

  .content {
    padding: 4px 2px 0;
  }

  .notes-list {
    grid-template-columns: repeat(2, 1fr);
  }
}
