/* ============================================================
   Model Pulse — hand-crafted iOS-inspired UI
   No framework, no template. SF-system stack, hairlines, blur.
   ============================================================ */

:root {
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
          "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;

  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 9px;

  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
}

/* ---------- themes ---------- */
:root[data-theme="light"] {
  --bg: #f2f2f7;
  --bg-elev: #ffffff;
  --bg-elev2: #f7f7fa;
  --bg-fill: rgba(120, 120, 128, 0.12);
  --bg-fill-strong: rgba(120, 120, 128, 0.18);
  --label: #1c1c1e;
  --label-2: rgba(60, 60, 67, 0.6);
  --label-3: rgba(60, 60, 67, 0.38);
  --separator: rgba(60, 60, 67, 0.12);
  --hairline: rgba(0, 0, 0, 0.06);
  --accent: #007aff;
  --green: #34c759;
  --red: #ff3b30;
  --orange: #ff9500;
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 0 0 0.5px var(--hairline);
  --nav-bg: rgba(255, 255, 255, 0.72);
  --sheet-bg: #f2f2f7f5;
  --canvas-bg: repeating-linear-gradient(45deg, #fafafc 0 12px, #f2f2f6 12px 24px);
}
:root[data-theme="dark"] {
  --bg: #000000;
  --bg-elev: #1c1c1e;
  --bg-elev2: #2c2c2e;
  --bg-fill: rgba(120, 120, 128, 0.24);
  --bg-fill-strong: rgba(120, 120, 128, 0.36);
  --label: #f5f5f7;
  --label-2: rgba(235, 235, 245, 0.6);
  --label-3: rgba(235, 235, 245, 0.38);
  --separator: rgba(84, 84, 88, 0.6);
  --hairline: rgba(255, 255, 255, 0.09);
  --accent: #0a84ff;
  --green: #30d158;
  --red: #ff453a;
  --orange: #ff9f0a;
  --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 0 0 0.5px var(--hairline);
  --nav-bg: rgba(22, 22, 24, 0.72);
  --sheet-bg: #1c1c1ef2;
  --canvas-bg: repeating-linear-gradient(45deg, #232326 0 12px, #1c1c1e 12px 24px);
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--label);
  font-size: 15px;
  line-height: 1.45;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

.wrap { max-width: 1120px; margin: 0 auto; padding: 0 20px 48px; }

/* ---------- nav bar (translucent, iOS style) ---------- */
.nav {
  position: sticky; top: 0; z-index: 50;
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  background: var(--nav-bg);
  border-bottom: 0.5px solid var(--hairline);
}
.nav-inner {
  max-width: 1120px; margin: 0 auto; padding: 14px 20px;
  display: flex; align-items: center; gap: 14px;
}
.nav-title {
  font-size: 17px; font-weight: 700; letter-spacing: -0.03em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.nav-sub { font-size: 12px; color: var(--label-2); white-space: nowrap; }
.nav-spacer { flex: 1; }

/* ---------- large title + sub ---------- */
.page-head { padding: 28px 0 6px; }
.large-title { font-size: 34px; font-weight: 800; letter-spacing: -0.035em; }
.page-sub { margin-top: 4px; color: var(--label-2); font-size: 14px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); display: inline-block; flex: none; }
.sync-dot.live { animation: pulse 1.6s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ---------- segmented control ---------- */
.seg {
  display: inline-flex; position: relative;
  background: var(--bg-fill);
  border-radius: var(--radius-sm); padding: 2px;
  max-width: 100%; overflow-x: auto; scrollbar-width: none;
}
.seg::-webkit-scrollbar { display: none; }
.seg-item {
  position: relative; z-index: 1;
  padding: 6px 16px; border-radius: 7px;
  font-size: 13px; font-weight: 600; color: var(--label);
  white-space: nowrap; transition: color 0.18s var(--ease);
}
.seg-item.active { color: var(--label); }
.seg-item:not(.active) { color: var(--label-2); }
.seg-pill {
  position: absolute; top: 2px; bottom: 2px; z-index: 0;
  background: var(--bg-elev);
  border-radius: 7px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12), 0 0 0 0.5px var(--hairline);
  transition: left 0.22s var(--ease), width 0.22s var(--ease);
}
[data-theme="dark"] .seg-pill { background: #636366; box-shadow: none; }

.filters { display: flex; gap: 12px; align-items: center; margin: 18px 0 6px; flex-wrap: wrap; }

/* ---------- icon buttons ---------- */
.icon-btn {
  width: 34px; height: 34px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--accent); background: transparent;
  transition: background 0.15s var(--ease);
  flex: none;
}
.icon-btn:hover { background: var(--bg-fill); }
.icon-btn:active { background: var(--bg-fill-strong); transform: scale(0.94); }
.icon-btn svg { width: 19px; height: 19px; stroke: currentColor; stroke-width: 1.8; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.spin svg { animation: rotate 0.9s linear infinite; }
@keyframes rotate { to { transform: rotate(360deg); } }

/* ---------- group section ---------- */
.group-section { margin-top: 26px; }
.group-head {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 4px;
}
.group-name { font-size: 20px; font-weight: 700; letter-spacing: -0.025em; }
.group-meta { font-size: 12.5px; color: var(--label-2); }
.group-badges { display: inline-flex; gap: 6px; margin-left: auto; align-items: center; }

.tag {
  font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 6px;
  background: var(--bg-fill); color: var(--label-2);
}
.tag.running { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.tag.paused { background: color-mix(in srgb, var(--orange) 14%, transparent); color: var(--orange); }

/* ---------- model cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; margin-top: 12px; }

.card {
  background: var(--bg-elev);
  border-radius: var(--radius-lg);
  box-shadow: var(--card-shadow);
  overflow: hidden;
  transition: transform 0.2s var(--ease);
  display: flex; flex-direction: column;
}
.card:active { transform: scale(0.985); }
.card-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px 10px; }
.model-name {
  font-family: var(--mono); font-size: 12.5px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0;
}
.status-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.status-dot.ok { background: var(--green); }
.status-dot.no_svg { background: var(--orange); }
.status-dot.error { background: var(--red); }
.status-dot.pending { background: var(--label-3); }
.status-dot.running { background: var(--accent); animation: pulse 1.2s infinite; }

.canvas {
  background: var(--canvas-bg);
  height: 170px; display: flex; align-items: center; justify-content: center;
  overflow: hidden; position: relative;
  border-top: 0.5px solid var(--hairline); border-bottom: 0.5px solid var(--hairline);
}
/* fit the whole drawing inside the frame — never cropped (preserveAspectRatio defaults to meet) */
.canvas > svg { width: 100%; height: 100%; }
.canvas .empty {
  color: var(--label-3); font-size: 12px; display: flex; flex-direction: column;
  align-items: center; gap: 6px; padding: 12px; text-align: center;
}
.canvas .empty svg { width: 26px; height: 26px; stroke: var(--label-3); stroke-width: 1.5; fill: none; }

.card-foot {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px 12px;
  font-size: 12px; color: var(--label-2);
}
.foot-main { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--label); }
.foot-time { margin-left: auto; font-variant-numeric: tabular-nums; }
.chev { width: 16px; height: 16px; stroke: var(--label-3); stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; transition: transform .18s var(--ease); }

.expand-btn {
  margin-top: 10px; padding: 8px 14px; border-radius: var(--radius-md);
  background: var(--bg-elev); box-shadow: var(--card-shadow);
  font-size: 13px; font-weight: 600; color: var(--accent);
  transition: transform 0.15s var(--ease);
}
.expand-btn:active { transform: scale(0.97); }

/* ---------- sparkline ---------- */
.spark { width: 100%; height: 34px; padding: 4px 6px 2px; }
.spark polyline { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.spark line { stroke: var(--separator); stroke-width: 0.5; }

/* ---------- bottom sheet ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0, 0, 0, 0.32);
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s var(--ease);
}
.overlay.show { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; z-index: 100; left: 50%; bottom: 0;
  width: min(720px, 100%); max-height: 86vh;
  transform: translate(-50%, 130%);   /* fully off-screen when closed */
  opacity: 0; visibility: hidden; pointer-events: none;
  background: var(--sheet-bg);
  backdrop-filter: blur(30px) saturate(180%);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  border-radius: 20px 20px 0 0;
  padding: 8px 20px 26px;
  display: flex; flex-direction: column;
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1),
              opacity 0.25s var(--ease),
              visibility 0s linear 0.32s;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.25);
}
.sheet.open {
  transform: translate(-50%, 0);
  opacity: 1; visibility: visible; pointer-events: auto;
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1),
              opacity 0.25s var(--ease),
              visibility 0s;
}
@media (min-width: 760px) {
  .sheet { bottom: 5vh; border-radius: 20px; }
}
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--label-3); margin: 6px auto 10px; flex: none; }
.sheet-title { font-size: 17px; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 2px; }
.sheet-sub { font-size: 12.5px; color: var(--label-2); margin-bottom: 12px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.hist-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; padding: 4px 0 8px; }
.hist-item { background: var(--bg-elev); border-radius: var(--radius-md); box-shadow: var(--card-shadow); overflow: hidden; }
.hist-item .canvas { height: 110px; }
.hist-item .h-foot { display: flex; justify-content: space-between; padding: 7px 10px; font-size: 11px; color: var(--label-2); font-variant-numeric: tabular-nums; }
.hist-item.sel { outline: 2px solid var(--accent); outline-offset: -2px; }
.sheet-close { position: absolute; top: 14px; right: 16px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translate(-50%, 20px);
  background: var(--bg-elev); color: var(--label);
  padding: 11px 20px; border-radius: 14px; font-size: 13.5px; font-weight: 600;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.22), 0 0 0 0.5px var(--hairline);
  opacity: 0; pointer-events: none; z-index: 200;
  transition: opacity 0.25s var(--ease), transform 0.25s var(--ease);
  max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { color: var(--red); }

/* ---------- footer note ---------- */
.footer-note { margin-top: 34px; color: var(--label-3); font-size: 12px; text-align: center; }

/* ---------- admin ---------- */
.form-card {
  background: var(--bg-elev); border-radius: var(--radius-lg);
  box-shadow: var(--card-shadow); padding: 20px;
  margin-top: 16px;
}
.form-title { font-size: 17px; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 14px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--label-2); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.field input[type="text"], .field input[type="password"], .field input[type="number"], .field textarea, .field select {
  width: 100%; padding: 11px 13px; border-radius: var(--radius-md);
  background: var(--bg-elev2); border: 0.5px solid var(--hairline);
  font-size: 14.5px; outline: none;
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.field textarea { min-height: 84px; resize: vertical; font-family: var(--mono); font-size: 12.5px; }
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3.5px color-mix(in srgb, var(--accent) 18%, transparent);
}
.field .hint { font-size: 11.5px; color: var(--label-3); margin-top: 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 11px 20px; border-radius: var(--radius-md);
  font-size: 14.5px; font-weight: 600; letter-spacing: -0.01em;
  background: var(--accent); color: #fff;
  transition: transform 0.15s var(--ease), opacity 0.15s var(--ease);
}
.btn:active { transform: scale(0.97); }
.btn.secondary { background: var(--bg-fill); color: var(--label); }
.btn.danger { background: color-mix(in srgb, var(--red) 12%, transparent); color: var(--red); }
.btn svg { width: 16px; height: 16px; stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; }

/* iOS switch */
.switch { position: relative; width: 50px; height: 30px; display: inline-block; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0; border-radius: 16px;
  background: var(--bg-fill-strong);
  transition: background 0.2s var(--ease);
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 26px; height: 26px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s var(--ease);
}
.switch input:checked + .track { background: var(--green); }
.switch input:checked + .track::after { transform: translateX(20px); }

/* admin group rows */
.g-row {
  background: var(--bg-elev); border-radius: var(--radius-lg); box-shadow: var(--card-shadow);
  padding: 16px 18px; margin-top: 12px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.g-row .g-name { font-weight: 700; font-size: 15.5px; }
.g-row .g-url { font-size: 12px; color: var(--label-2); font-family: var(--mono); word-break: break-all; }
.g-row .g-stats { font-size: 12px; color: var(--label-2); }
.g-actions { margin-left: auto; display: flex; gap: 8px; }
.empty-state {
  margin-top: 24px; padding: 44px 20px; text-align: center; color: var(--label-2);
  background: var(--bg-elev); border-radius: var(--radius-lg); box-shadow: var(--card-shadow);
}
.empty-state svg { width: 40px; height: 40px; stroke: var(--label-3); stroke-width: 1.5; fill: none; margin-bottom: 10px; }

@media (max-width: 560px) {
  .wrap { padding: 0 14px 40px; }
  .nav-inner { padding: 12px 14px; }
  .large-title { font-size: 28px; }
  .row2 { grid-template-columns: 1fr; }
  .cards { grid-template-columns: 1fr; }
}
