/* ============================================================
   Optical Power Converter — MTS-5800 instrument homage
   ============================================================ */

:root {
  /* brand chrome (constant across themes — like the device title bar) */
  --brand-1: #0a5c96;
  --brand-2: #00a7e1;
  --cyan:    #00a7e1;
  --cyan-hi: #5cd2ff;
  --green:   #2fd07a;
  --amber:   #f5a623;

  /* sizing */
  --maxw: 480px;
  --radius: 14px;
  --gap: 12px;

  --mono: "SF Mono", "JetBrains Mono", "Roboto Mono", ui-monospace,
          "DejaVu Sans Mono", Menlo, Consolas, monospace;
  --ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
        Arial, sans-serif;
}

/* ---- dark theme (default) ---- */
:root[data-theme="dark"] {
  --bg:        #0b121b;
  --bg-grad:   radial-gradient(120% 80% at 50% -10%, #15212f 0%, #0b121b 60%);
  --panel:     #16212e;
  --panel-2:   #101a25;
  --panel-brd: #243648;
  --ink:       #eaf2f9;
  --ink-dim:   #8aa0b4;
  --label:     #6f879c;
  --rail-idle: #2a3a4b;
  --readout:   #ffffff;
  --key-bg:    #1b2836;
  --key-brd:   #2c4053;
  --key-ink:   #c4d6e6;
  --shadow:    0 1px 0 rgba(255,255,255,.03) inset, 0 8px 24px rgba(0,0,0,.45);
}

/* ---- light theme ---- */
:root[data-theme="light"] {
  --bg:        #e7edf2;
  --bg-grad:   radial-gradient(120% 80% at 50% -10%, #f4f8fb 0%, #dde6ed 70%);
  --panel:     #ffffff;
  --panel-2:   #f1f5f8;
  --panel-brd: #cdd9e3;
  --ink:       #11202e;
  --ink-dim:   #5a7287;
  --label:     #6b8197;
  --rail-idle: #c3d0db;
  --readout:   #0c1a26;
  --key-bg:    #f3f7fa;
  --key-brd:   #cfdae3;
  --key-ink:   #284558;
  --shadow:    0 1px 2px rgba(16,40,60,.06), 0 8px 22px rgba(20,50,80,.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--ui);
  color: var(--ink);
  background: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  display: flex;
  justify-content: center;
  align-items: stretch;
  -webkit-font-smoothing: antialiased;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.device {
  width: 100%;
  max-width: var(--maxw);
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* ---------------- Title bar (brand chrome) ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: linear-gradient(180deg, var(--brand-2) 0%, var(--brand-1) 100%);
  color: #fff;
  border-bottom: 2px solid rgba(0,0,0,.25);
  box-shadow: 0 3px 10px rgba(0,0,0,.30);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.vmark {
  font-size: 16px;
  letter-spacing: -3px;
  color: #fff;
  text-shadow: 0 1px 1px rgba(0,0,0,.35);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.05; min-width: 0; }
.brand-name {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .16em;
  opacity: .92;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-model {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: .02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* soft key buttons */
.softkey {
  font-family: var(--ui);
  border: 1px solid rgba(255,255,255,.45);
  background: rgba(255,255,255,.14);
  color: #fff;
  border-radius: 9px;
  padding: 6px 10px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  white-space: nowrap;
  transition: background .15s ease, transform .05s ease;
}
.softkey:active { transform: translateY(1px); }
.softkey:hover { background: rgba(255,255,255,.24); }
.topbtns { display: flex; align-items: center; gap: 8px; flex: none; }
.link-key { text-decoration: none; font-family: var(--ui); }
.link-key[aria-label="Install help"] { padding: 6px 11px; font-size: 13px; }
.theme-ico {
  width: 14px; height: 14px; border-radius: 50%;
  background:
    radial-gradient(circle at 32% 32%, #fff 0 46%, transparent 47%),
    conic-gradient(#fff 0 50%, #0a3a5c 50% 100%);
  box-shadow: inset 0 0 0 1.5px rgba(255,255,255,.7);
}

/* ---------------- Screen ---------------- */
.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: 14px 14px calc(16px + env(safe-area-inset-bottom));
}

.status-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 2px;
}
.pill {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .04em;
  color: var(--cyan);
  border: 1px solid color-mix(in srgb, var(--cyan) 45%, transparent);
  background: color-mix(in srgb, var(--cyan) 12%, transparent);
  padding: 4px 11px;
  border-radius: 999px;
}
.status-meta {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 10px; font-weight: 700; letter-spacing: .14em;
  color: var(--ink-dim);
}
.status-meta .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 7px var(--green);
  animation: blink 2.4s ease-in-out infinite;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.35} }

/* ---------------- Result cells ---------------- */
.cell {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: border-color .18s ease, box-shadow .18s ease;
}
.rail {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 5px;
  background: var(--rail-idle);
  transition: background .18s ease, box-shadow .18s ease;
}
.cell[data-state="ok"]     .rail { background: var(--green); box-shadow: 0 0 12px color-mix(in srgb, var(--green) 70%, transparent); }
.cell[data-state="active"] .rail { background: var(--cyan);  box-shadow: 0 0 12px color-mix(in srgb, var(--cyan) 80%, transparent); }
.cell[data-state="warn"]   .rail { background: var(--amber); box-shadow: 0 0 12px color-mix(in srgb, var(--amber) 70%, transparent); }
.cell[data-state="active"] {
  border-color: color-mix(in srgb, var(--cyan) 60%, var(--panel-brd));
  box-shadow: var(--shadow), 0 0 0 1px color-mix(in srgb, var(--cyan) 40%, transparent);
}

.cell-inner { padding: 12px 14px 13px 18px; }
.cell-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
  min-height: 22px;
}
.cell-head label {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .15em;
  color: var(--label);
}
.cell-unit {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  color: var(--cyan);
}

.readout {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--readout);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: clamp(34px, 11vw, 46px);
  line-height: 1.04;
  letter-spacing: .01em;
  padding: 2px 0 0;
  outline: none;
}
.readout::placeholder { color: var(--rail-idle); }
.readout:focus { color: var(--readout); }

.autorange {
  margin-top: 7px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-dim);
  letter-spacing: .01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* unit selector */
.unitsel { display: inline-flex; border: 1px solid var(--panel-brd); border-radius: 8px; overflow: hidden; }
.unitsel button {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  padding: 4px 8px;
  border: none;
  border-right: 1px solid var(--panel-brd);
  background: var(--panel-2);
  color: var(--ink-dim);
  cursor: pointer;
  min-width: 30px;
  transition: background .12s ease, color .12s ease;
}
.unitsel button:last-child { border-right: none; }
.unitsel button:hover { color: var(--ink); }
.unitsel button.active {
  background: var(--cyan);
  color: #042430;
}

/* ---------------- Soft-key preset row ---------------- */
.softrow {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 7px;
}
.preset {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 700;
  padding: 11px 0;
  border-radius: 10px;
  border: 1px solid var(--key-brd);
  background: linear-gradient(180deg,
              color-mix(in srgb, var(--key-bg) 100%, #fff 4%),
              var(--key-bg));
  color: var(--key-ink);
  cursor: pointer;
  transition: transform .05s ease, border-color .15s ease, color .15s ease;
}
.preset:hover { border-color: var(--cyan); color: var(--cyan); }
.preset:active { transform: translateY(1px); }
.preset.clear { color: var(--amber); }
.preset.clear:hover { border-color: var(--amber); color: var(--amber); }

/* ---------------- Formula footer ---------------- */
.formula {
  margin-top: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px 12px;
  padding-top: 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-dim);
}
.formula sub { font-size: .7em; }
.formula sup { font-size: .7em; }
.formula .sep { opacity: .5; }

/* ---------------- focus ring ---------------- */
:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; border-radius: 4px; }

/* value-update flash */
.flash { animation: flash .4s ease; }
@keyframes flash {
  0%   { background: color-mix(in srgb, var(--cyan) 16%, var(--panel)); }
  100% { background: var(--panel); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* small phones (iPhone SE 1st gen, 320px) */
@media (max-width: 340px) {
  .readout { font-size: 30px; }
  .preset { font-size: 13px; padding: 10px 0; }
  .unitsel button { min-width: 26px; padding: 4px 5px; }
  .brand-model { font-size: 15px; }
}

/* larger screens — center the “instrument” and give it presence */
@media (min-width: 600px) {
  body { align-items: flex-start; padding: 28px; }
  .device {
    margin-block: auto;          /* center vertically, but collapse to 0 on overflow
                                    so the title bar/buttons stay reachable */
    border-radius: 22px;
    overflow: hidden;
    box-shadow: 0 24px 70px rgba(0,0,0,.45), 0 0 0 1px var(--panel-brd);
    min-height: 0;
    background: var(--bg);
  }
  .topbar { border-radius: 0; }
}
