/* Level & Ruler — a brass bubble level and a steel rule, made out of the phone.
 * One Page Toys tool family, self-contained, theme-aware (light/dark).
 * The instruments are drawn on canvas and stay physical in both themes (brass
 * is brass at night); the bench they sit on and the chrome follow the theme. */

@import url("https://fonts.googleapis.com/css2?family=Archivo:wght@500;600;700;800;900&family=Barlow+Condensed:wght@500;600;700&family=Inter:wght@400;500;600;700&display=swap");

:root{
  --font-display: "Archivo", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-engrave: "Barlow Condensed", "Archivo", system-ui, sans-serif;
  --ink: #1f1b16;
  --bg: #f5f2ec;
  --surface: #ffffff;
  --surface-2: #efe9df;
  --muted: #6c6457;
  --line: #e4ddd1;
  --line-strong: #cfc5b4;
  --accent: #94661c;
  --accent-hover: #7a5214;
  --accent-soft: rgba(184, 137, 58, 0.16);
  --ok: #1f8a4c;
  --radius: 18px;
  --radius-sm: 12px;
  --radius-pill: 999px;
  --shadow: 0 2px 8px rgba(40, 30, 14, 0.06);
  --shadow-md: 0 8px 22px rgba(40, 30, 14, 0.10);
  --shadow-lg: 0 16px 40px rgba(40, 30, 14, 0.14);
  --glass: rgba(255, 255, 255, 0.74);
  /* the bench the instruments rest on */
  --bench-1: #ebe5da;
  --bench-2: #ddd5c6;
  --bench-ink: #2b251d;
  --bench-muted: rgba(43, 37, 29, 0.62);
}
:root[data-theme="dark"]{
  --ink: #efe8dc;
  --bg: #12100d;
  --surface: #1c1915;
  --surface-2: #25211b;
  --muted: #a89e8f;
  --line: #2c2720;
  --line-strong: #41392e;
  --accent: #e2b45c;
  --accent-hover: #f0c878;
  --accent-soft: rgba(226, 180, 92, 0.16);
  --ok: #5fd68d;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 10px 30px -6px rgba(0, 0, 0, 0.55);
  --shadow-lg: 0 18px 48px -10px rgba(0, 0, 0, 0.65);
  --glass: rgba(28, 25, 21, 0.66);
  --bench-1: #24201a;
  --bench-2: #15130f;
  --bench-ink: #efe6d6;
  --bench-muted: rgba(239, 230, 214, 0.6);
}

*, *::before, *::after{ box-sizing: border-box; }
html, body{ height: 100%; -webkit-tap-highlight-color: transparent; }
body{
  margin: 0; min-height: 100%;
  display: flex; flex-direction: column;
  font-family: var(--font-sans); color: var(--ink);
  background: radial-gradient(64% 46% at 50% 0%, var(--accent-soft), transparent 70%), var(--bg);
  overflow-x: hidden; -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
.sr-only{ position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- Top bar ---------- */
.topbar{
  position: relative; z-index: 3;
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px; flex-wrap: wrap;
  padding: 14px clamp(16px, 4vw, 30px);
  padding-top: max(14px, env(safe-area-inset-top));
  padding-left: max(clamp(16px, 4vw, 30px), env(safe-area-inset-left));
  padding-right: max(clamp(16px, 4vw, 30px), env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.brand{ display: inline-flex; align-items: center; gap: 10px; font-family: var(--font-display); font-weight: 800; font-size: 0.86rem; letter-spacing: 0.16em; text-transform: uppercase; color: var(--ink); text-decoration: none; }
.brand__mark{ width: 1.1em; height: 1.1em; border: 1.5px solid var(--ink); border-radius: 6px; background: linear-gradient(90deg, var(--ink) 0 50%, var(--surface) 50% 100%); }
.topbar__right{ display: inline-flex; align-items: center; gap: 12px; }
.topbar__tag{ font-family: var(--font-display); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); }
.iconbtn{ appearance: none; display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; font-size: 1rem; line-height: 1; border: 1.5px solid var(--line-strong); border-radius: var(--radius-pill); background: var(--surface); color: var(--ink); cursor: pointer; box-shadow: var(--shadow); transition: transform 140ms ease, box-shadow 140ms ease, border-color 140ms ease; }
.iconbtn:hover{ transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--ink); }
.iconbtn:focus-visible{ outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 5px var(--accent); }
.theme-toggle__sun, .theme-toggle__moon, .theme-toggle__auto{ display: none; }
:root[data-theme-mode="system"] .theme-toggle__auto{ display: inline; }
:root[data-theme-mode="light"] .theme-toggle__sun{ display: inline; }
:root[data-theme-mode="dark"] .theme-toggle__moon{ display: inline; }

/* ---------- Stage ---------- */
.stage{
  position: relative; z-index: 2; flex: 1 0 auto;
  display: flex; flex-direction: column; align-items: center;
  gap: 22px;
  padding: clamp(16px, 3vw, 34px) 16px clamp(48px, 6vw, 72px);
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: max(clamp(48px, 6vw, 72px), env(safe-area-inset-bottom));
}
.lr{ width: 100%; max-width: 620px; }
.lr__head{ text-align: center; }
.eyebrow{ margin: 0 0 2px; font-family: var(--font-display); font-size: 0.7rem; font-weight: 700; letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted); }
.headline{
  margin: 0 0 14px; font-family: var(--font-display); font-weight: 900;
  font-size: clamp(1.55rem, 6vw, 2.4rem); line-height: 1.05; letter-spacing: -0.02em; color: var(--ink);
}

/* ---------- Tabs ---------- */
.tabs{
  position: relative; display: grid; grid-template-columns: 1fr 1fr;
  margin: 0 auto 14px; max-width: 320px; padding: 4px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-pill);
  background: var(--surface-2);
}
.tabs__btn{
  position: relative; z-index: 1; appearance: none; border: 0; background: transparent;
  min-height: 40px; border-radius: var(--radius-pill);
  font-family: var(--font-display); font-size: 0.92rem; font-weight: 800; letter-spacing: 0.02em;
  color: var(--muted); cursor: pointer; transition: color 180ms ease;
}
.tabs__btn[aria-selected="true"]{ color: var(--bg); }
.tabs__btn:focus-visible{ outline: none; box-shadow: 0 0 0 3px var(--accent); }
.tabs__slide{
  position: absolute; z-index: 0; top: 4px; bottom: 4px; left: 4px; width: calc(50% - 4px);
  border-radius: var(--radius-pill); background: var(--ink); box-shadow: var(--shadow-md);
  transition: transform 260ms cubic-bezier(.3,1.3,.5,1);
}
.tabs[data-tab="ruler"] .tabs__slide{ transform: translateX(100%); }

.view[hidden]{ display: none; }

/* ---------- Level bench ---------- */
.bench{
  position: relative; width: 100%;
  height: clamp(360px, 114vw, 480px);
  border: 1px solid var(--line); border-radius: var(--radius);
  background:
    radial-gradient(90% 70% at 50% 18%, rgba(255, 244, 214, 0.22), transparent 70%),
    linear-gradient(170deg, var(--bench-1), var(--bench-2));
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255,255,255,0.25);
  overflow: hidden;
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
:root[data-theme="dark"] .bench{
  background:
    radial-gradient(80% 60% at 50% 22%, rgba(255, 200, 120, 0.10), transparent 70%),
    linear-gradient(170deg, var(--bench-1), var(--bench-2));
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255,255,255,0.05);
}
.bench__canvas{ position: absolute; inset: 0; width: 100%; height: 100%; display: block; -webkit-touch-callout: none; }
.bench.is-demo .bench__canvas{ cursor: grab; }
.bench.is-demo.is-dragging .bench__canvas{ cursor: grabbing; }
.bench__canvas:focus-visible{ outline: none; box-shadow: inset 0 0 0 3px var(--accent); border-radius: var(--radius); }

.bench-note{
  margin: 10px 0 0; padding: 0 8px;
  font-size: 0.8rem; line-height: 1.45; text-align: center; color: var(--muted);
}
.bench-note[hidden]{ display: none; }

.gate{
  position: absolute; inset: auto 16px 16px 16px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 16px; border-radius: var(--radius-sm); text-align: center;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--line-strong); box-shadow: var(--shadow-md);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.gate[hidden]{ display: none; }
.gate__text{ margin: 0; font-size: 0.86rem; line-height: 1.45; color: var(--ink); }

/* calibration card, docked to the bench's bottom edge */
.calib{
  position: absolute; left: 12px; right: 12px; bottom: 12px;
  display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 10px 14px;
  padding: 14px; border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line-strong); box-shadow: var(--shadow-md);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  animation: rise 260ms cubic-bezier(.2,.9,.3,1.2);
}
.calib[hidden]{ display: none; }
.calib__text{ margin: 0; font-size: 0.86rem; line-height: 1.42; color: var(--ink); }
.calib__actions{ grid-column: 1 / -1; display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.calib__ring{
  --p: 0; width: 38px; height: 38px; border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--p) * 1turn), var(--line-strong) 0);
  display: grid; place-items: center;
}
.calib__ring span{ width: 28px; height: 28px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-size: 0.7rem; font-weight: 800; color: var(--accent); font-family: var(--font-display); }
.calib.is-done .calib__ring{ background: var(--ok); }
.calib.is-done .calib__ring span::before{ content: "✓"; color: var(--ok); font-size: 0.95rem; }
@keyframes rise{ from{ transform: translateY(14px); opacity: 0; } to{ transform: none; opacity: 1; } }

/* ---------- Console under the level ---------- */
.console{ margin-top: 14px; display: flex; flex-direction: column; gap: 12px; }
.console__row{ display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }
.console__row--actions{ gap: 10px; }
.console__row[hidden]{ display: none; }

.seg{
  display: inline-grid; grid-auto-flow: column; gap: 2px; padding: 3px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-pill); background: var(--surface-2);
}
.seg__btn{
  appearance: none; border: 0; background: transparent; min-height: 38px; padding: 0 14px;
  border-radius: var(--radius-pill); cursor: pointer;
  font-family: var(--font-display); font-size: 0.8rem; font-weight: 700; color: var(--muted);
  transition: background 160ms ease, color 160ms ease;
}
.seg__btn[aria-checked="true"]{ background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg__btn.is-sensed:not([aria-checked="true"]){ color: var(--accent); }
.seg__btn:focus-visible{ outline: none; box-shadow: 0 0 0 3px var(--accent); }

.sound-toggle{ width: 44px; height: 44px; }
.sound-toggle .ico{ width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sound-toggle .ico--off{ display: none; }
.sound-toggle[aria-pressed="false"] .ico--on{ display: none; }
.sound-toggle[aria-pressed="false"] .ico--off{ display: block; }
.sound-toggle[aria-pressed="false"]{ color: var(--muted); }


.span{
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px 8px;
  padding: 10px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--glass);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.span__k{ font-family: var(--font-display); font-size: 0.95rem; font-weight: 700; color: var(--muted); }
.chip{
  appearance: none; min-height: 34px; padding: 0 12px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--line-strong); background: var(--surface); color: var(--ink);
  font-family: var(--font-display); font-size: 0.8rem; font-weight: 800; cursor: pointer;
  transition: border-color 140ms ease, transform 140ms ease;
}
.chip:hover{ border-color: var(--accent); }
.chip:active{ transform: scale(0.96); }
.chip:focus-visible{ outline: none; box-shadow: 0 0 0 3px var(--accent); }

.cal-state, .scale-state{ margin: 0; text-align: center; font-size: 0.76rem; color: var(--muted); line-height: 1.4; }
.cal-state b, .scale-state b{ color: var(--ink); font-weight: 700; }
.scale-state.is-true b{ color: var(--ok); }

/* ---------- Buttons ---------- */
.btn{
  appearance: none; font-family: var(--font-display); font-size: 0.9rem; font-weight: 700;
  min-height: 44px; padding: 0 20px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--line-strong); background: var(--surface); color: var(--ink);
  cursor: pointer; box-shadow: var(--shadow);
  transition: transform 160ms ease, box-shadow 160ms ease, color 160ms ease, border-color 160ms ease, background 160ms ease;
}
.btn:hover{ transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--ink); }
.btn:active{ transform: translateY(0) scale(0.98); }
.btn--primary{ background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn--quiet{ background: transparent; box-shadow: none; color: var(--muted); }
.btn--small{ min-height: 40px; padding: 0 14px; font-size: 0.82rem; }
.btn:disabled{ opacity: 0.45; cursor: default; transform: none; box-shadow: none; }
.btn:focus-visible{ outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 5px var(--accent); }
.btn[hidden]{ display: none; }

/* ---------- Ruler ---------- */
.measure{
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 12px;
  margin-bottom: 10px;
}
.measure[hidden]{ display: none; }
.measure__read{ display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; min-width: 0; }
.measure__big{
  font-family: var(--font-display); font-weight: 900; font-size: clamp(1.9rem, 9vw, 2.6rem);
  line-height: 1; letter-spacing: -0.02em; color: var(--ink); font-variant-numeric: tabular-nums;
}
.measure__small{ font-family: var(--font-display); font-weight: 700; font-size: 1rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.measure__tools{ display: flex; gap: 8px; align-items: center; }
.seg--small .seg__btn{ min-height: 34px; padding: 0 12px; }
.scale-state{ grid-column: 1 / -1; text-align: left; }

.cardcal{
  margin-bottom: 10px; padding: 12px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--accent); background: var(--accent-soft);
  animation: rise 240ms cubic-bezier(.2,.9,.3,1.2);
}
.cardcal[hidden]{ display: none; }
.cardcal__text{ margin: 0 0 10px; font-size: 0.86rem; line-height: 1.42; color: var(--ink); }
.cardcal__row{ display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cardcal__row .iconbtn{ width: 40px; height: 40px; font-size: 1.2rem; font-weight: 700; }
.cardcal__grow{ flex: 1 1 auto; }

.rule{
  position: relative; width: 100%;
  height: 300px;
  border-radius: 10px; overflow: hidden;
  box-shadow: var(--shadow-lg);
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  background: #c9ced5;
}
.rule.is-vertical{ height: max(300px, calc(100svh - 360px)); }
.rule__canvas{ position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; }
.rule.is-dragging .rule__canvas{ cursor: grabbing; }

.jaw{
  position: absolute; left: 0; top: 0; pointer-events: none;
  border-radius: 6px;
}
.jaw:focus-visible{ outline: none; box-shadow: inset 0 0 0 3px var(--accent), 0 0 0 2px rgba(255,255,255,0.6); }
.rule.is-vertical .jaw{ width: 100%; height: 44px; }
.rule:not(.is-vertical) .jaw{ height: 100%; width: 44px; }
.rule.is-calibrating .jaw{ display: none; }

/* ---------- Notes ---------- */
.notes{
  margin-top: 26px; padding-top: 18px; border-top: 1px solid var(--line);
  font-size: 0.86rem; line-height: 1.55; color: var(--muted);
}
.notes p{ margin: 0 0 10px; }
.notes strong{ color: var(--ink); font-weight: 700; }
.notes__private{ font-size: 0.78rem; }

/* share.js mounts its pill here; the theme token keeps it readable in light and dark */
.share-row{ display: flex; justify-content: center; margin: -6px 0 -6px; }
.share-row[hidden]{ display: none; }
.share-row .opt-share{ --ops-fg: var(--ink); }
.backlink{ font-family: var(--font-display); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); text-decoration: none; border-bottom: 1px solid transparent; transition: color 140ms ease, border-color 140ms ease; }
.backlink:hover{ color: var(--ink); border-bottom-color: var(--accent); }

/* The shared badges dock bottom-right, out of the bench. While the ruler is
   out on a touch screen they step aside: the whole screen is the measuring
   surface, and a floating button over it is in the way. */
.opt-tipjar.opt-tipjar{ top: auto; bottom: max(14px, env(safe-area-inset-bottom)); transform: none; }
.opt-fs.opt-fs{ top: auto; bottom: calc(max(14px, env(safe-area-inset-bottom)) + 54px); transform: none; }
.opt-tipjar.opt-tipjar.tj-nudge{ animation: none; }
@media (pointer: coarse){
  body.is-ruler .opt-tipjar, body.is-ruler .opt-fs, body.is-ruler .opt-tickets{ display: none !important; }
}

/* on a phone the ruler wants every pixel of height: the heading steps aside
   (still there for screen readers and search) while the ruler is out */
@media (max-width: 560px){
  body.is-ruler .lr__head{ position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}
@media (max-width: 420px){
  .topbar__tag{ font-size: 0.64rem; letter-spacing: 0.12em; }
  .seg__btn{ padding: 0 11px; }
  .measure{ grid-template-columns: 1fr; }
  .measure__tools{ justify-content: flex-start; }
}
@media (orientation: landscape) and (max-height: 540px){
  /* a phone on its long edge: the bench should fit the screen without a scroll */
  .topbar{ padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: 8px; }
  .stage{ padding-top: 10px; gap: 14px; }
  .lr__head{ position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .tabs{ margin-bottom: 8px; }
  .tabs__btn{ min-height: 36px; }
  .bench{ height: max(220px, calc(100svh - 124px)); }
  .rule{ height: max(200px, calc(100svh - 190px)); }
}
@media (prefers-reduced-motion: reduce){
  .btn, .iconbtn, .tabs__slide, .tabs__btn, .seg__btn, .chip{ transition: none; }
  .calib, .cardcal{ animation: none; }
}
