@layer reset, tokens, base, components, utilities;

/* =========================================================================
   FONTS
   ========================================================================= */

@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400;
  font-display: block;
  src: url('fonts/inter-latin-400-normal.woff2') format('woff2');
}
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 500;
  font-display: block;
  src: url('fonts/inter-latin-500-normal.woff2') format('woff2');
}
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 600;
  font-display: block;
  src: url('fonts/inter-latin-600-normal.woff2') format('woff2');
}

/* =========================================================================
   TOKENS
   ========================================================================= */

@layer tokens {
  :root {
    --tint:           rgba(252, 246, 237, 0.35);
    --glass:          rgba(255, 255, 255, 0.58);
    --glass-strong:   rgba(255, 255, 255, 0.78);
    --glass-soft:     rgba(255, 255, 255, 0.36);
    --glass-hover:    rgba(255, 255, 255, 0.72);

    --border-bright:  rgba(255, 255, 255, 0.85);
    --border-soft:    rgba(255, 255, 255, 0.55);
    --border-thin:    rgba(40, 30, 20, 0.06);

    --text:        #1C1A17;
    --text-muted:  #6E665C;
    --text-dim:    #A39C92;

    --accent:        #FF6B3D;
    --accent-strong: #E85A2D;
    --accent-soft:   rgba(255, 107, 61, 0.14);
    --accent-glow:   rgba(255, 107, 61, 0.42);

    --clinical:       #3D8DD3;
    --clinical-band:  rgba(61, 141, 211, 0.07);
    --sleep:          #D85F4A;
    --sleep-band:     rgba(216, 95, 74, 0.08);
    --over:           #FA114F;
    --good:           #2EA46A;

    --inner-highlight:
      inset 0 1px 0 rgba(255, 255, 255, 0.95),
      inset 0 0 0 1px rgba(255, 255, 255, 0.18);
    --shadow-card:
      0 1px 1px rgba(20, 14, 8, 0.04),
      0 6px 20px rgba(20, 14, 8, 0.06);
    --shadow-hero:
      0 1px 1px rgba(20, 14, 8, 0.04),
      0 12px 36px rgba(255, 107, 61, 0.08),
      0 6px 20px rgba(20, 14, 8, 0.06);

    --font-sans: 'Inter', 'SF Pro Text', -apple-system, BlinkMacSystemFont,
                 'Segoe UI Variable', 'Segoe UI', system-ui, sans-serif;
    --font-display: 'SF Pro Display', 'Segoe UI Variable Display',
                    'Inter', -apple-system, system-ui, sans-serif;

    --t-md:      14px;
    --t-base:    12px;
    --t-sm:      11px;
    --t-xs:      10px;

    --s-1: 4px;  --s-2: 6px;  --s-3: 10px;  --s-4: 14px;
    --s-5: 18px; --s-6: 22px; --s-7: 28px;  --s-8: 36px;

    --ease-out:    cubic-bezier(0.22, 1, 0.36, 1);
    --ease-soft:   cubic-bezier(0.4, 0, 0.2, 1);
    --ease-spring: cubic-bezier(0.34, 1.3, 0.64, 1);
    --dur-fast: 120ms;
    --dur:      220ms;
    --dur-slow: 600ms;

    --radius-xl:  20px;
    --radius-lg:  16px;
    --radius:     10px;
    --radius-sm:  7px;
  }
}

/* =========================================================================
   RESET + BASE
   ========================================================================= */

@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  button { background: none; border: none; color: inherit; font: inherit; cursor: pointer; }
  input, select { font: inherit; color: inherit; }
  ul { list-style: none; }
}

@layer base {
  html, body {
    background: transparent;
    color: var(--text);
    font-family: var(--font-sans);
    font-size: var(--t-base);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-feature-settings: 'cv11', 'ss01';
    user-select: none;
  }
  body {
    min-height: 100vh;
    overflow: hidden;
    padding: var(--s-2);
    display: flex;
    flex-direction: column;
    gap: var(--s-2);
    background:
      radial-gradient(120% 80% at 30% 0%, rgba(255, 196, 145, 0.20) 0%, transparent 60%),
      radial-gradient(80% 60% at 100% 100%, rgba(255, 138, 90, 0.10) 0%, transparent 60%),
      var(--tint);
  }
  .num { font-variant-numeric: tabular-nums slashed-zero; }
}

/* =========================================================================
   COMPONENTS
   ========================================================================= */

@layer components {

  /* ---- Header ---- */
  header {
    -webkit-app-region: drag;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--s-1) var(--s-2);
    flex-shrink: 0;
  }
  header .wordmark {
    font-size: var(--t-xs);
    font-weight: 700;
    letter-spacing: 0.20em;
    text-transform: uppercase;
    color: var(--text-muted);
    padding-left: var(--s-2);
  }
  header .actions {
    -webkit-app-region: no-drag;
    display: flex;
    gap: var(--s-1);
  }

  .icon-btn {
    width: 24px; height: 24px;
    display: inline-flex; align-items: center; justify-content: center;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    transition: color var(--dur) var(--ease-out),
                background-color var(--dur) var(--ease-out),
                transform var(--dur-fast) var(--ease-soft);
  }
  .icon-btn:hover { color: var(--text); background: var(--glass-hover); }
  .icon-btn:active { transform: scale(0.93); }
  .icon-btn.active { color: var(--accent); background: var(--accent-soft); }
  .icon-btn.active svg { transform: rotate(-30deg); }
  .icon-btn svg {
    width: 13px; height: 13px;
    stroke-width: 1.75;
    transition: transform var(--dur) var(--ease-out);
  }
  .icon-btn.danger:hover { color: #fff; background: var(--over); }

  /* ---- Tabs: Apple segmented control ---- */
  .tabs {
    -webkit-app-region: no-drag;
    display: flex;
    gap: 2px;
    padding: 3px;
    background: rgba(40, 30, 20, 0.06);
    border-radius: 999px;
    box-shadow: inset 0 1px 1px rgba(40, 30, 20, 0.04);
    flex-shrink: 0;
  }
  .tab {
    flex: 1;
    height: 24px;
    border-radius: 999px;
    color: var(--text-muted);
    font-size: var(--t-xs);
    font-weight: 600;
    letter-spacing: 0.02em;
    transition: color var(--dur-fast) var(--ease-soft),
                background-color var(--dur-fast) var(--ease-soft);
  }
  .tab:hover { color: var(--text); }
  .tab.active {
    background: #fff;
    color: var(--text);
    box-shadow:
      0 1px 1px rgba(40, 30, 20, 0.06),
      0 1px 3px rgba(40, 30, 20, 0.05);
  }

  /* ---- Tab panels ---- */
  .tab-panel {
    display: none;
    flex: 1;
    min-height: 0;
    flex-direction: column;
    gap: var(--s-2);
    overflow-y: auto;
    overflow-x: hidden;
  }
  .tab-panel.active { display: flex; }
  .tab-panel::-webkit-scrollbar { width: 4px; }
  .tab-panel::-webkit-scrollbar-track { background: transparent; }
  .tab-panel::-webkit-scrollbar-thumb { background: rgba(40, 30, 20, 0.12); border-radius: 2px; }

  /* ---- Card primitive ---- */
  .card {
    position: relative;
    background: var(--glass);
    backdrop-filter: blur(40px) saturate(180%);
    -webkit-backdrop-filter: blur(40px) saturate(180%);
    border-radius: var(--radius-lg);
    padding: var(--s-3);
    box-shadow: var(--shadow-card);
    flex-shrink: 0;
  }
  .card::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    box-shadow: var(--inner-highlight);
  }
  .card > * { position: relative; z-index: 1; }
  .card-title {
    font-size: var(--t-xs);
    font-weight: 600;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--text-dim);
    margin-bottom: var(--s-2);
  }

  /* ---- HERO card ---- */
  .hero-card {
    padding: var(--s-3);
    box-shadow: var(--shadow-hero);
  }
  .hero-main {
    display: flex;
    align-items: center;
    gap: var(--s-3);
  }
  .hero-ring-wrap {
    position: relative;
    width: 96px;
    height: 96px;
    flex-shrink: 0;
    transform-origin: center;
  }
  .hero-ring-wrap.pulse { animation: ringPulse 700ms var(--ease-spring); }
  @keyframes ringPulse {
    0%   { transform: scale(1); }
    35%  { transform: scale(1.06); }
    100% { transform: scale(1); }
  }
  .hero-ring { width: 96px; height: 96px; display: block; overflow: visible; }
  .hero-ring .ring-track {
    fill: none;
    stroke: rgba(40, 30, 20, 0.08);
    stroke-width: 7;
  }
  .hero-ring .ring-fill {
    fill: none;
    stroke-width: 7;
    stroke-linecap: round;
    transition: stroke-dashoffset 600ms var(--ease-out);
    filter: drop-shadow(0 1px 2px rgba(255, 107, 61, 0.25));
  }
  .ring-content {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
  }
  .hero-num {
    font-family: var(--font-display);
    font-variant-numeric: tabular-nums slashed-zero;
    font-weight: 600;
    font-size: 26px;
    line-height: 1;
    letter-spacing: -0.03em;
    color: var(--accent);
    transition: color var(--dur-slow) var(--ease-out);
  }
  .hero-num.below { color: var(--text-muted); }
  .hero-unit {
    font-size: 8px;
    font-weight: 700;
    color: var(--text-dim);
    letter-spacing: 0.12em;
    text-transform: uppercase;
  }

  .hero-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--s-1);
  }

  .next-dose {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: var(--accent-soft);
    border: 1px solid rgba(255, 107, 61, 0.18);
    border-radius: var(--radius-sm);
    color: var(--accent-strong);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.5);
  }
  .next-dose.urgent {
    background: rgba(255, 107, 61, 0.20);
    border-color: rgba(255, 107, 61, 0.32);
  }
  .next-dose.hidden { display: none; }
  .next-dose .ndi { width: 12px; height: 12px; stroke-width: 1.75; flex-shrink: 0; }
  .nd-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
  .nd-label {
    font-size: 8px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-muted);
  }
  .nd-value {
    font-size: var(--t-xs);
    font-weight: 600;
    color: var(--accent-strong);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .last-dose {
    font-size: var(--t-xs);
    color: var(--text-dim);
    font-weight: 500;
  }
  .last-dose.hidden { display: none; }
  .last-dose .num { color: var(--text-muted); font-variant-numeric: tabular-nums; }

  .status {
    color: var(--text-muted);
    font-size: var(--t-xs);
    line-height: 1.4;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .status .key { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 500; }
  .status-line {
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .status-line svg { width: 10px; height: 10px; stroke-width: 1.75; flex-shrink: 0; }
  .status-line .dot {
    width: 5px; height: 5px;
    border-radius: 50%;
    background: currentColor;
    flex-shrink: 0;
    box-shadow: 0 0 0 2px rgba(255,255,255,0.4);
  }
  .status-line.warn { color: var(--sleep); }
  .status-line.warn .key { color: var(--sleep); }
  .status-line.ok { color: var(--good); }
  .status-line.ok .key { color: var(--text); }
  .status-line.muted { color: var(--text-muted); }

  /* ---- CHART card ---- */
  .chart-card {
    padding: var(--s-2);
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }
  .chart-wrap {
    position: relative;
    flex: 1;
    min-height: 140px;
    cursor: crosshair;
  }
  .chart-legend {
    position: absolute;
    top: 4px; right: 6px;
    display: flex;
    gap: 8px;
    font-size: 9px;
    color: var(--text-muted);
    font-weight: 600;
    letter-spacing: 0.04em;
    pointer-events: none;
    z-index: 2;
  }
  .leg-item {
    display: flex; align-items: center; gap: 4px;
    background: rgba(255,255,255,0.55);
    padding: 2px 6px;
    border-radius: 999px;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.7), 0 1px 1px rgba(0,0,0,0.03);
    backdrop-filter: blur(8px);
  }
  .leg-dot { width: 5px; height: 5px; border-radius: 50%; }
  .leg-dot.clinical { background: var(--clinical); }
  .leg-dot.sleep    { background: var(--sleep); }
  .leg-item .num { color: var(--text); font-variant-numeric: tabular-nums; }

  .now-dot {
    position: absolute;
    width: 9px; height: 9px;
    border-radius: 50%;
    background: var(--accent);
    pointer-events: none;
    transform: translate(-50%, -50%);
    box-shadow:
      0 0 0 3px rgba(255,255,255,0.95),
      0 0 16px var(--accent-glow);
    animation: nowPulse 1.8s var(--ease-out) infinite;
    z-index: 3;
    transition: left 600ms var(--ease-out), top 600ms var(--ease-out);
  }
  .now-dot.hidden { display: none; }
  .now-dot.scrubbing { animation: none; opacity: 0.85; transition: none; }
  @keyframes nowPulse {
    0%, 100% {
      box-shadow:
        0 0 0 3px rgba(255,255,255,0.95),
        0 0 14px rgba(255, 107, 61, 0.30);
    }
    50% {
      box-shadow:
        0 0 0 3px rgba(255,255,255,0.95),
        0 0 28px rgba(255, 107, 61, 0.65);
    }
  }

  .scrub-label {
    position: absolute;
    pointer-events: none;
    background: rgba(28, 26, 23, 0.92);
    color: #FCF7F0;
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 10px;
    font-weight: 500;
    display: flex;
    gap: 8px;
    transform: translate(-50%, -130%);
    white-space: nowrap;
    z-index: 4;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  }
  .scrub-label.hidden { display: none; }
  .scrub-label .st { color: #C7BFB1; }
  .scrub-label .sv { color: var(--accent); font-weight: 600; }
  .scrub-label::after {
    content: '';
    position: absolute;
    left: 50%; bottom: -4px;
    transform: translateX(-50%);
    border: 4px solid transparent;
    border-top-color: rgba(28, 26, 23, 0.92);
    border-bottom: 0;
  }

  /* ---- Compact log card (LOG tab) ---- */
  .compact-log { position: relative; }
  .compact-log-row {
    display: flex;
    align-items: center;
    gap: var(--s-2);
  }

  .formulation-pill {
    height: 28px;
    padding: 0 8px 0 12px;
    border-radius: 999px;
    background: var(--glass-strong);
    border: 1px solid var(--border-soft);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text);
    font-size: var(--t-sm);
    font-weight: 500;
    cursor: pointer;
    transition: background-color var(--dur-fast) var(--ease-soft),
                border-color var(--dur-fast) var(--ease-soft),
                transform 60ms var(--ease-soft);
    flex-shrink: 0;
    min-width: 112px;
    justify-content: space-between;
  }
  .formulation-pill:hover { border-color: rgba(255, 107, 61, 0.3); }
  .formulation-pill:active { transform: scale(0.97); }
  .formulation-pill.open { background: #fff; border-color: var(--accent); }
  .formulation-pill .chev {
    width: 11px; height: 11px;
    stroke-width: 2;
    color: var(--text-muted);
    transition: transform var(--dur) var(--ease-out);
  }
  .formulation-pill.open .chev { transform: rotate(180deg); }

  .formulation-menu {
    position: absolute;
    top: calc(var(--s-3) + 32px);
    left: var(--s-3);
    z-index: 10;
    min-width: 140px;
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(28px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.85);
    border-radius: var(--radius);
    box-shadow:
      0 1px 1px rgba(20, 14, 8, 0.04),
      0 12px 36px rgba(20, 14, 8, 0.14);
    padding: 4px;
    animation: menuIn 160ms var(--ease-out);
  }
  @keyframes menuIn {
    from { opacity: 0; transform: scale(0.96) translateY(-4px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
  }
  .formulation-menu.hidden { display: none; }
  .formulation-menu li {
    padding: 6px 10px;
    border-radius: 6px;
    font-size: var(--t-sm);
    color: var(--text);
    cursor: pointer;
    transition: background-color 80ms var(--ease-soft), color 80ms var(--ease-soft);
  }
  .formulation-menu li:hover {
    background: var(--accent-soft);
    color: var(--accent-strong);
  }
  .formulation-menu li.selected {
    background: var(--accent);
    color: #fff;
    font-weight: 500;
  }

  .dose-chips {
    display: flex;
    gap: var(--s-1);
    flex-wrap: wrap;
  }
  .dose-chips.quick {
    flex: 1;
    justify-content: flex-end;
    flex-wrap: nowrap;
  }
  .chip {
    height: 26px;
    padding: 0 10px;
    border-radius: 999px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    font-variant-numeric: tabular-nums;
    font-size: var(--t-sm);
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
    transition: background-color var(--dur-fast) var(--ease-soft),
                border-color var(--dur-fast) var(--ease-soft),
                color var(--dur-fast) var(--ease-soft),
                transform 60ms var(--ease-soft);
  }
  .chip:hover {
    background: var(--glass-strong);
    border-color: rgba(255, 107, 61, 0.4);
    color: var(--accent-strong);
  }
  .chip:active { transform: scale(0.96); }
  .dose-chips.quick .chip {
    flex: 1;
    min-width: 0;
    padding: 0 6px;
  }

  /* Custom dose row */
  .custom-card { }
  .custom-grid {
    display: grid;
    grid-template-columns: 1fr 1fr auto auto;
    gap: var(--s-2);
    align-items: center;
  }
  .custom-grid input {
    height: 30px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 0 var(--s-2);
    font-size: var(--t-sm);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
    outline: none;
    transition: border-color var(--dur-fast) var(--ease-soft);
    font-variant-numeric: tabular-nums;
    width: 100%;
    min-width: 0;
  }
  .custom-grid input:focus { border-color: var(--accent); }
  .custom-grid .ghost {
    height: 30px;
    padding: 0 10px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: var(--t-sm);
    transition: color var(--dur-fast) var(--ease-soft);
  }
  .custom-grid .ghost:hover { color: var(--text); }
  .custom-grid .primary {
    height: 30px;
    padding: 0 var(--s-4);
    border-radius: var(--radius-sm);
    background: linear-gradient(180deg, var(--accent) 0%, var(--accent-strong) 100%);
    color: #fff;
    font-weight: 600;
    font-size: var(--t-sm);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.35),
      0 1px 2px rgba(232, 90, 45, 0.4);
    transition: filter var(--dur-fast) var(--ease-soft),
                transform 60ms var(--ease-soft);
  }
  .custom-grid .primary:hover { filter: brightness(1.07); }
  .custom-grid .primary:active { transform: scale(0.96); }

  .warning {
    margin-top: var(--s-2);
    padding: var(--s-2) var(--s-3);
    border-radius: var(--radius-sm);
    background: rgba(216, 95, 74, 0.12);
    border: 1px solid rgba(216, 95, 74, 0.28);
    color: var(--sleep);
    font-size: var(--t-xs);
    line-height: 1.4;
    display: flex;
    gap: var(--s-2);
    align-items: flex-start;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
  }
  .warning svg { width: 12px; height: 12px; margin-top: 1px; flex-shrink: 0; stroke-width: 1.75; }
  .warning.hidden { display: none; }

  /* History */
  .history ul { display: flex; flex-direction: column; gap: var(--s-1); }
  .history li {
    height: 32px;
    padding: 0 var(--s-2);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: var(--radius-sm);
    transition: background-color var(--dur-fast) var(--ease-soft);
  }
  .history li:hover { background: var(--glass-hover); }
  .history .info { display: flex; align-items: center; gap: var(--s-3); min-width: 0; }
  .history .time {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    font-size: var(--t-sm);
    font-weight: 500;
    width: 42px;
    flex-shrink: 0;
  }
  .history .type { color: var(--text); font-size: var(--t-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .history .lvl {
    color: var(--text-dim);
    font-variant-numeric: tabular-nums;
    font-size: var(--t-xs);
  }
  .history .remove {
    width: 22px; height: 22px;
    color: var(--text-dim);
    border-radius: var(--radius-sm);
    display: inline-flex; align-items: center; justify-content: center;
    opacity: 0;
    transition: opacity var(--dur-fast) var(--ease-soft), color var(--dur-fast) var(--ease-soft);
  }
  .history .remove svg { width: 12px; height: 12px; stroke-width: 1.75; }
  .history li:hover .remove { opacity: 1; }
  .history .remove:hover { color: var(--over); }
  .history .empty {
    padding: var(--s-5) 0;
    text-align: center;
    color: var(--text-dim);
    font-size: var(--t-sm);
  }

  /* Settings */
  .settings-grid {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: var(--s-3) var(--s-4);
    align-items: center;
  }
  .settings-grid label { color: var(--text-muted); font-size: var(--t-sm); }
  .settings-grid input {
    height: 28px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 0 var(--s-2);
    font-variant-numeric: tabular-nums;
    font-size: var(--t-sm);
    font-weight: 500;
    width: 96px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
    outline: none;
    transition: border-color var(--dur-fast) var(--ease-soft);
  }
  .settings-grid input:focus { border-color: var(--accent); }
  .footer-row {
    margin-top: var(--s-3);
    display: flex;
    justify-content: flex-end;
  }
  .danger-link {
    color: var(--text-muted);
    font-size: var(--t-xs);
    display: inline-flex;
    align-items: center;
    gap: var(--s-1);
    padding: var(--s-1) var(--s-2);
    border-radius: var(--radius-sm);
    transition: color var(--dur-fast) var(--ease-soft),
                background-color var(--dur-fast) var(--ease-soft);
  }
  .danger-link:hover { color: var(--over); background: rgba(250, 17, 79, 0.08); }
  .danger-link svg { width: 12px; height: 12px; stroke-width: 1.75; }

  /* ====================================================================
     NEW: schedule, follow button, chart range toggle, modal, toggles, export
     ==================================================================== */

  /* Missed-dose action rows */
  .missed-slots {
    margin-top: var(--s-3);
    display: flex; flex-direction: column;
    gap: var(--s-1);
  }
  .missed-slots.hidden { display: none; }
  .missed-slot {
    display: flex;
    align-items: center;
    gap: var(--s-2);
    padding: 6px 8px 6px 10px;
    background: rgba(255, 107, 61, 0.10);
    border: 1px solid rgba(255, 107, 61, 0.22);
    border-radius: var(--radius-sm);
    font-size: var(--t-xs);
    color: var(--accent-strong);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.5);
  }
  .missed-slot .ms-icon { width: 12px; height: 12px; stroke-width: 1.75; flex-shrink: 0; }
  .missed-slot .ms-text { flex: 1; min-width: 0; }
  .missed-slot .ms-text .num { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 500; }
  .missed-slot button {
    height: 22px;
    padding: 0 8px;
    border-radius: var(--radius-sm);
    font-size: var(--t-xs);
    font-weight: 600;
    transition: filter var(--dur-fast), transform 60ms;
  }
  .missed-slot .ms-log {
    background: var(--accent);
    color: #fff;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.3);
  }
  .missed-slot .ms-log:hover { filter: brightness(1.07); }
  .missed-slot .ms-skip {
    background: transparent;
    color: var(--text-muted);
  }
  .missed-slot .ms-skip:hover { color: var(--text); }
  .missed-slot button:active { transform: scale(0.96); }

  /* Latest-safe status line variant */
  .status-line.deadline { color: var(--text); }
  .status-line.deadline .key { color: var(--accent-strong); }
  .status-line.deadline-tight { color: var(--sleep); }
  .status-line.deadline-tight .key { color: var(--sleep); }

  /* Stats list (Setup → Your data) */
  .stats-list {
    display: flex; flex-direction: column;
    gap: var(--s-2);
  }
  .stats-list .empty {
    padding: var(--s-3) 0;
    text-align: center;
    color: var(--text-dim);
    font-size: var(--t-xs);
  }
  .stat-row {
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    gap: var(--s-2);
    align-items: center;
    padding: 6px 8px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.6);
    font-size: var(--t-xs);
  }
  .stat-row .sr-name { color: var(--text); font-weight: 500; }
  .stat-row .sr-count {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
  }
  .stat-row .sr-rating {
    color: var(--accent);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-width: 38px;
    text-align: right;
  }
  .stat-row .sr-rating.dim { color: var(--text-dim); }
  .stat-row .sr-mg {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    background: rgba(40, 30, 20, 0.06);
    padding: 2px 6px;
    border-radius: 999px;
  }

  /* Chart range toggle (mini segmented control inside chart card) */
  .chart-header {
    display: flex; justify-content: flex-end;
    padding: 0 var(--s-1) var(--s-1);
  }
  .range-toggle {
    display: flex;
    background: rgba(40, 30, 20, 0.06);
    border-radius: 999px;
    padding: 2px;
    gap: 1px;
  }
  .rt {
    height: 20px;
    padding: 0 10px;
    border-radius: 999px;
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.02em;
    transition: color var(--dur-fast), background-color var(--dur-fast);
  }
  .rt.active {
    background: #fff;
    color: var(--text);
    box-shadow: 0 1px 1px rgba(40,30,20,0.06);
  }

  /* Schedule list */
  .schedule-list {
    display: flex; flex-direction: column;
    gap: var(--s-2);
  }
  .schedule-list .empty {
    padding: var(--s-3) 0;
    text-align: center;
    color: var(--text-dim);
    font-size: var(--t-xs);
  }
  .schedule-row {
    display: grid;
    grid-template-columns: 72px 1fr 60px 24px;
    gap: var(--s-2);
    align-items: center;
    padding: 0;
  }
  .schedule-row input[type="time"],
  .schedule-row input[type="number"] {
    height: 28px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 0 var(--s-2);
    font-size: var(--t-sm);
    font-variant-numeric: tabular-nums;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.6);
    outline: none;
    width: 100%;
    min-width: 0;
  }
  .schedule-row input:focus { border-color: var(--accent); }
  .schedule-row .sl-form-pill {
    height: 28px;
    padding: 0 8px 0 10px;
    border-radius: 999px;
    background: var(--glass-strong);
    border: 1px solid var(--border-soft);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    color: var(--text);
    font-size: var(--t-xs);
    font-weight: 500;
    cursor: pointer;
    min-width: 0;
    width: 100%;
  }
  .schedule-row .sl-form-pill svg {
    width: 10px; height: 10px;
    stroke-width: 2;
    color: var(--text-muted);
    flex-shrink: 0;
  }
  .schedule-row .sl-form-label {
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .schedule-row .sl-remove {
    width: 24px; height: 24px;
    color: var(--text-dim);
    border-radius: var(--radius-sm);
    display: inline-flex; align-items: center; justify-content: center;
    transition: color var(--dur-fast), background-color var(--dur-fast);
  }
  .schedule-row .sl-remove:hover { color: var(--over); background: rgba(250, 17, 79, 0.08); }
  .schedule-row .sl-remove svg { width: 12px; height: 12px; stroke-width: 1.75; }

  .add-row {
    margin-top: var(--s-3);
    width: 100%;
    height: 28px;
    border-radius: var(--radius-sm);
    background: transparent;
    border: 1px dashed rgba(40, 30, 20, 0.18);
    color: var(--text-muted);
    font-size: var(--t-xs);
    font-weight: 500;
    display: flex; align-items: center; justify-content: center; gap: 6px;
    transition: color var(--dur-fast), border-color var(--dur-fast);
  }
  .add-row:hover { color: var(--accent); border-color: var(--accent); }
  .add-row svg { width: 11px; height: 11px; stroke-width: 1.75; }

  /* Toggle switches */
  .toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 0;
    font-size: var(--t-sm);
    color: var(--text);
  }
  .switch {
    position: relative;
    display: inline-block;
    width: 32px; height: 19px;
    flex-shrink: 0;
  }
  .switch input { opacity: 0; width: 0; height: 0; }
  .switch .slider {
    position: absolute; inset: 0;
    background: rgba(40, 30, 20, 0.14);
    border-radius: 999px;
    transition: background-color var(--dur) var(--ease-soft);
    cursor: pointer;
    box-shadow: inset 0 1px 1px rgba(40, 30, 20, 0.06);
  }
  .switch .slider::before {
    content: '';
    position: absolute;
    width: 15px; height: 15px;
    left: 2px; top: 2px;
    background: #fff;
    border-radius: 50%;
    transition: transform var(--dur) var(--ease-spring);
    box-shadow: 0 1px 2px rgba(40, 30, 20, 0.18);
  }
  .switch input:checked + .slider { background: var(--accent); }
  .switch input:checked + .slider::before { transform: translateX(13px); }

  /* Export */
  .export-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0 var(--s-2);
    font-size: var(--t-sm);
    color: var(--text);
  }
  .export-row .export-label { color: var(--text-muted); }
  .export-row select {
    height: 28px;
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 0 var(--s-2);
    font-size: var(--t-sm);
    font-weight: 500;
    cursor: pointer;
    outline: none;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
  }
  .export-buttons {
    display: flex;
    gap: var(--s-2);
    margin-top: var(--s-2);
  }
  .ghost-pill, .primary-pill {
    flex: 1;
    height: 32px;
    border-radius: var(--radius-sm);
    font-size: var(--t-sm);
    font-weight: 600;
    transition: filter var(--dur-fast), transform 60ms;
  }
  .ghost-pill {
    background: var(--glass-soft);
    border: 1px solid var(--border-soft);
    color: var(--text);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
  }
  .ghost-pill:hover { background: var(--glass-strong); }
  .primary-pill {
    background: linear-gradient(180deg, var(--accent) 0%, var(--accent-strong) 100%);
    color: #fff;
    border: none;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.35),
      0 1px 2px rgba(232, 90, 45, 0.4);
  }
  .primary-pill:hover { filter: brightness(1.07); }
  .primary-pill:active, .ghost-pill:active { transform: scale(0.97); }

  /* Modal (focus rating) */
  .modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: modalIn 180ms var(--ease-out);
  }
  .modal.hidden { display: none; }
  .modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(28, 26, 23, 0.30);
    backdrop-filter: blur(8px);
  }
  .modal-card {
    position: relative;
    z-index: 1;
    background: rgba(255, 255, 255, 0.90);
    backdrop-filter: blur(28px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.85);
    border-radius: var(--radius-lg);
    padding: var(--s-4);
    box-shadow:
      0 1px 1px rgba(20, 14, 8, 0.04),
      0 14px 40px rgba(20, 14, 8, 0.18);
    max-width: 280px;
    width: calc(100% - 32px);
    text-align: center;
  }
  @keyframes modalIn {
    from { opacity: 0; }
    to   { opacity: 1; }
  }
  .modal-title {
    font-size: var(--t-base);
    color: var(--text);
    font-weight: 500;
    margin-bottom: var(--s-3);
  }
  .rating-stars {
    display: flex;
    justify-content: center;
    gap: 4px;
    margin-bottom: var(--s-3);
  }
  .rating-stars .star {
    width: 32px; height: 32px;
    border-radius: 8px;
    font-size: 22px;
    line-height: 1;
    color: rgba(40, 30, 20, 0.18);
    transition: color var(--dur-fast) var(--ease-soft),
                background-color var(--dur-fast),
                transform 60ms;
  }
  .rating-stars .star:hover,
  .rating-stars .star.hovered,
  .rating-stars .star.selected {
    color: var(--accent);
  }
  .rating-stars .star:active { transform: scale(0.92); }
  .ghost-link {
    background: none;
    color: var(--text-muted);
    font-size: var(--t-xs);
    padding: 4px 10px;
    border-radius: var(--radius-sm);
  }
  .ghost-link:hover { color: var(--text); }
}

/* =========================================================================
   MOBILE OVERRIDES  (unlayered → highest priority)
   Turns the desktop widget into a full-screen, touch-first phone app.
   The desktop Electron build relied on OS acrylic behind a transparent body;
   on the web we paint a warm paper background so the glass cards read right.
   ========================================================================= */

:root {
  color-scheme: light;
  /* Bump the type scale a step for arm's-length phone reading + touch. */
  --t-md:   16px;
  --t-base: 14px;
  --t-sm:   13px;
  --t-xs:   12px;
}

html {
  background:
    radial-gradient(120% 70% at 30% 0%, rgba(255, 196, 145, 0.28) 0%, transparent 60%),
    radial-gradient(90% 60% at 100% 100%, rgba(255, 138, 90, 0.14) 0%, transparent 60%),
    #FCF6ED;
  -webkit-text-size-adjust: 100%;
}

body {
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  gap: 8px;
  background: none;                 /* gradient lives on <html> now */
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  padding:
    calc(env(safe-area-inset-top) + 8px)
    calc(env(safe-area-inset-right) + 12px)
    calc(env(safe-area-inset-bottom) + 8px)
    calc(env(safe-area-inset-left) + 12px);
}

/* Header — no window chrome to drag on a phone. */
header { -webkit-app-region: none; padding: 2px 4px 0; }
header .wordmark { font-size: var(--t-xs); }

/* Segmented tabs — finger-sized. */
.tabs { padding: 4px; }
.tab { height: 40px; font-size: var(--t-sm); }

/* Smooth momentum scrolling inside panels. */
.tab-panel { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; gap: 10px; padding-bottom: 4px; }

/* Bigger hero ring on the roomier screen. */
.hero-ring-wrap, .hero-ring { width: 112px; height: 112px; }
.hero-num { font-size: 30px; }
.hero-unit { font-size: 9px; }

/* Chart: don't let the page scroll while scrubbing the curve. */
.chart-card { min-height: 230px; }
.chart-wrap { min-height: 200px; cursor: default; touch-action: none; }

/* Log tab — chunky tap targets. */
.formulation-pill { height: 40px; font-size: var(--t-base); min-width: 132px; }
.chip { height: 40px; padding: 0 16px; font-size: var(--t-base); }
.dose-chips.quick { gap: 6px; }
.dose-chips.quick .chip { padding: 0 8px; }
.formulation-menu li { padding: 10px 12px; font-size: var(--t-base); }
.custom-grid { grid-template-columns: 1fr 1fr auto auto; gap: 8px; }
.custom-grid input { height: 44px; font-size: var(--t-base); }
.custom-grid .ghost { height: 44px; font-size: var(--t-sm); }
.custom-grid .primary { height: 44px; font-size: var(--t-base); padding: 0 18px; }

/* History — rows always show the delete control (no hover on touch). */
.history li { height: 46px; }
.history .remove { opacity: 1; width: 28px; height: 28px; }
.history .remove svg { width: 14px; height: 14px; }

/* Setup tab. */
.settings-grid { gap: 14px 16px; }
.settings-grid label { font-size: var(--t-base); }
.settings-grid input { height: 40px; width: 118px; font-size: var(--t-base); }
.schedule-row { grid-template-columns: 78px 1fr 64px 32px; gap: 8px; }
.schedule-row input[type="time"],
.schedule-row input[type="number"],
.schedule-row .sl-form-pill { height: 40px; font-size: var(--t-sm); }
.schedule-row .sl-remove { width: 32px; height: 32px; }
.add-row { height: 40px; font-size: var(--t-sm); }

.toggle-row { padding: 10px 0; font-size: var(--t-base); }
.switch { width: 46px; height: 28px; }
.switch .slider::before { width: 22px; height: 22px; left: 3px; top: 3px; }
.switch input:checked + .slider::before { transform: translateX(18px); }
.notif-note {
  margin-top: 8px;
  font-size: var(--t-xs);
  line-height: 1.45;
  color: var(--text-dim);
}

.export-row select { height: 40px; font-size: var(--t-base); }
.ghost-pill, .primary-pill { height: 46px; font-size: var(--t-base); }
.danger-link { font-size: var(--t-sm); padding: 8px 10px; }

/* Focus-rating modal — large stars for thumbs. */
.rating-stars .star { width: 46px; height: 46px; font-size: 32px; border-radius: 10px; }
.modal-title { font-size: var(--t-md); }
.ghost-link { font-size: var(--t-sm); padding: 8px 14px; }

/* Buttons should feel instant, not hover-driven. */
@media (hover: none) {
  .chip:hover, .formulation-pill:hover, .history li:hover { background: var(--glass-soft); }
}

/* --- Fix #1: formulation dropdown must paint OVER the Custom-dose card.
   Each glass card is its own stacking context, so lifting the card (not just
   the menu) is what makes the overflowing dropdown win. --- */
.compact-log.menu-open { z-index: 30; }
.formulation-menu { top: 58px; min-width: 160px; max-height: 320px; overflow-y: auto; }

/* --- Fix #4: chart "Now" recenter button (appears once you've panned away) --- */
.chart-header { justify-content: flex-end; gap: 8px; align-items: center; }
.chart-now-btn {
  display: inline-flex; align-items: center; gap: 5px;
  margin-right: auto;                 /* sit at the left; range toggle stays right */
  height: 26px; padding: 0 11px;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid rgba(255, 107, 61, 0.22);
  color: var(--accent-strong);
  font-size: var(--t-xs); font-weight: 600;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
}
.chart-now-btn svg { width: 13px; height: 13px; stroke-width: 2; }
.chart-now-btn.hidden { display: none; }

/* --- Language toggle (segmented control, matches the tab bar) --- */
.lang-toggle {
  display: flex; gap: 2px; padding: 3px;
  background: rgba(40, 30, 20, 0.06);
  border-radius: 999px;
  box-shadow: inset 0 1px 1px rgba(40, 30, 20, 0.04);
}
.lt {
  flex: 1; height: 38px; border-radius: 999px;
  color: var(--text-muted); font-size: var(--t-sm); font-weight: 600;
  transition: color var(--dur-fast), background-color var(--dur-fast);
}
.lt.active {
  background: #fff; color: var(--text);
  box-shadow: 0 1px 1px rgba(40, 30, 20, 0.06), 0 1px 3px rgba(40, 30, 20, 0.05);
}

