/* Shock Pulse Workbench — V2
   Warm neutral workbench: beige page, light cards, olive accent, orange waveform. */

:root {
  --page: #EADFCF;
  --card: #FAF6F0;
  --field: #FFFFFF;
  --border: rgba(84, 66, 40, .12);
  --border-strong: rgba(84, 66, 40, .24);
  --shadow: 0 1px 2px rgba(70, 50, 25, .06), 0 6px 20px rgba(70, 50, 25, .06);

  --ink: #1E1C19;
  --ink-2: #45403A;
  --muted: #6B645A;
  --grid: rgba(60, 45, 25, .08);

  --olive: #5C6B35;
  --olive-hover: #4F5D2C;
  --on-olive: #FBF8F2;
  --olive-tint: #E7E6D3;

  --orange: #D65A26;
  --orange-ink: #B2461A;
  --orange-fill: rgba(214, 90, 38, .15);

  --tag: #2F2C28;
  --on-tag: #F7F2EA;
  --error: #A3261A;

  --font: "Inter", "Segoe UI", system-ui, sans-serif;
  --radius: 16px;
  --radius-sm: 10px;
  --drawer-bar: 52px;
  --gutter: 32px;
  --ease: cubic-bezier(.3, .7, .2, 1);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--page);
  color: var(--ink);
  font: 400 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--drawer-bar) + 20px);
}
h1, h2, h3, p, ul, dl, dd, fieldset { margin: 0; padding: 0; }
ul { list-style: none; }
fieldset { border: 0; min-width: 0; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--olive); outline-offset: 2px; }
output, input, table, .meta dd, .tag b, .area-label b { font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- header ---------- */
.page-head { padding: 18px var(--gutter) 12px; }
.page-head h1 { font-size: 30px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.page-head p { color: var(--muted); margin-top: 2px; }

/* ---------- layout ---------- */
.workbench {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 16px;
  padding: 0 var(--gutter);
  min-height: 0;
}

/* ---------- control bar ---------- */
.controls {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 14px 0;
  padding: 14px 24px 12px;
}
.ctl { display: grid; gap: 6px; padding: 0 28px; border-left: 1px solid var(--border); }
.ctl-shape { padding-left: 0; border-left: 0; }
.ctl > label, .ctl legend { font-weight: 600; font-size: 15px; color: var(--ink); padding: 0; }
.ctl legend { margin-bottom: 6px; }

.segmented { display: flex; gap: 8px; }
.seg { position: relative; cursor: pointer; }
.seg input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.seg span {
  display: inline-flex; align-items: center; gap: 10px;
  height: 42px; padding: 0 18px;
  border-radius: var(--radius-sm);
  background: #EFE7DA;
  border: 1px solid transparent;
  font-weight: 500;
  transition: background-color .2s var(--ease), color .2s var(--ease);
}
.seg svg { width: 30px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.seg:hover span { background: #E7DDCC; }
.seg input:checked + span { background: var(--olive); color: var(--on-olive); border-color: var(--olive-hover); }
.seg input:focus-visible + span { outline: 2px solid var(--olive); outline-offset: 2px; }

.field {
  display: flex; align-items: center;
  width: 210px; height: 42px;
  background: var(--field);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.field:hover { border-color: rgba(84, 66, 40, .4); }
.field:focus-within { border-color: var(--olive); box-shadow: 0 0 0 3px rgba(92, 107, 53, .18); }
.field.invalid { border-color: var(--error); box-shadow: 0 0 0 3px rgba(163, 38, 26, .12); }
.field input {
  flex: 1; min-width: 0; height: 100%;
  border: 0; outline: none; background: transparent;
  padding: 0 8px 0 14px;
  font-size: 17px; font-weight: 500; text-align: center;
}
.field .unit {
  padding: 0 12px;
  color: var(--muted);
  border-left: 1px solid var(--border);
  line-height: 24px;
}
.unit-select { height: 100%; border: 0; border-left: 1px solid var(--border); border-radius: 0 8px 8px 0; background: transparent; font: inherit; color: var(--muted); cursor: pointer; }
.unit-select:focus-visible { outline: 2px solid var(--olive); outline-offset: -2px; }
.err { font-size: 12px; color: var(--error); max-width: 210px; }
.err:empty { display: none; }

.units {
  margin-left: auto;
  align-self: center;
  font-weight: 500;
  color: var(--ink-2);
  padding-left: 28px;
}

/* ---------- waveform ---------- */
.plot-card { display: grid; min-height: 440px; overflow: hidden; }
.plot { position: relative; min-height: 440px; }
#wave { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; }
#wave:focus-visible { outline-offset: -4px; border-radius: var(--radius); }

.handle {
  position: absolute; z-index: 3;
  width: 26px; height: 26px; margin: -13px 0 0 -13px;
  border-radius: 50%;
  background: var(--orange);
  border: 4px solid #FFFFFF;
  box-shadow: 0 0 0 1.5px var(--orange), 0 2px 6px rgba(70, 40, 20, .25);
  touch-action: none;
  transition: transform .15s var(--ease);
}
#peak-handle { cursor: ns-resize; }
#dur-handle { cursor: ew-resize; }
.handle:hover, .handle.dragging { transform: scale(1.15); }
.handle:focus-visible { outline: 2px solid var(--olive); outline-offset: 4px; }

.tag {
  position: absolute; z-index: 4; pointer-events: none;
  padding: 5px 12px;
  border-radius: 8px;
  background: var(--tag); color: var(--on-tag);
  font-size: 14px; white-space: nowrap;
}
.tag b { font-weight: 700; margin: 0 3px; }
#peak-tag { transform: translate(-50%, calc(-100% - 20px)); }
#dur-tag { transform: translate(-10%, calc(-100% - 22px)); }
#dur-tag.flip { transform: translate(-90%, calc(-100% - 22px)); }

.area-label {
  position: absolute; z-index: 2; pointer-events: none;
  transform: translate(-50%, -50%);
  display: grid; justify-items: center;
  color: var(--orange-ink);
}
.area-label span { font-size: 15px; font-weight: 500; }
.area-label b { font-size: 21px; font-weight: 700; }

.readout {
  position: absolute; z-index: 5; pointer-events: none;
  padding: 5px 10px; border-radius: 8px;
  background: var(--field); border: 1px solid var(--border-strong);
  font-size: 12.5px; white-space: nowrap; display: none;
}
.readout.show { display: block; }
.plot-msg {
  position: absolute; left: 24px; top: 14px; z-index: 6;
  color: var(--error); background: var(--field);
  border: 1px solid rgba(163, 38, 26, .35);
  padding: 6px 12px; border-radius: 8px; font-size: 13px; font-weight: 500;
}
.plot-msg:empty { display: none; }
.is-invalid .results .value, .is-invalid .area-label { opacity: .4; }

/* ---------- result chain ---------- */
.results {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 40px minmax(0, 1fr) 40px minmax(0, 1fr);
  align-items: stretch;
}
.flow { align-self: center; justify-self: center; width: 24px; height: 24px; fill: none; stroke: var(--muted); stroke-width: 1.6; }
.result { padding: 16px 28px 18px; display: grid; align-content: start; gap: 2px; }
.result h3 { font-size: 16px; font-weight: 500; color: var(--ink-2); }
.value { display: flex; align-items: baseline; gap: 12px; margin-top: 4px; }
.value output { font-size: 38px; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; transition: opacity .15s; }
.value .u { font-size: 20px; color: var(--muted); }
.sub { color: var(--muted); font-size: 15px; }
.sub.small { font-size: 13px; }

.result.primary { background: var(--olive); border-color: var(--olive-hover); color: var(--on-olive); }
.result.primary h3, .result.primary .sub, .result.primary .u { color: rgba(251, 248, 242, .88); }
.result.primary .value output { font-size: 48px; }

/* ---------- drawer ---------- */
.drawer {
  position: fixed; z-index: 20;
  left: var(--gutter); right: var(--gutter); bottom: 12px;
  display: flex; flex-direction: column-reverse;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.drawer.open { box-shadow: 0 -10px 40px rgba(70, 50, 25, .18); }
.drawer-bar { display: flex; align-items: center; height: var(--drawer-bar); padding: 0 12px; gap: 4px; }
.drawer-toggle {
  width: 36px; height: 36px; border: 0; border-radius: 8px; background: transparent; cursor: pointer;
  display: grid; place-items: center;
}
.drawer-toggle svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 2; transition: transform .25s var(--ease); }
.drawer.open .drawer-toggle svg { transform: rotate(180deg); }
.drawer-toggle:hover, .drawer-bar [role="tab"]:hover { background: #EFE7DA; }
.drawer-bar [role="tab"] {
  height: 36px; padding: 0 16px; border: 0; border-radius: 8px; background: transparent; cursor: pointer;
  font-weight: 500; font-size: 15px;
}
.drawer.open .drawer-bar [role="tab"][aria-selected="true"] { background: var(--olive-tint); font-weight: 600; }
.sep { width: 1px; height: 20px; background: var(--border-strong); margin: 0 4px; }

.drawer-body {
  display: none;
  height: min(46vh, 440px);
  padding: 18px 24px 4px;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}
.drawer.open .drawer-body { display: grid; }
.drawer-body [role="tabpanel"] { min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 12px; }
.drawer-body [role="tabpanel"][hidden] { display: none; }

.meta { display: flex; gap: 36px; }
.meta div { display: grid; }
.meta dt { font-size: 12.5px; color: var(--muted); }
.meta dd { font-weight: 600; }
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--field); max-width: 560px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  position: sticky; top: 0; background: #F3EDE3;
  text-align: right; font-weight: 600; padding: 9px 20px; border-bottom: 1px solid var(--border-strong);
}
tbody td { text-align: right; padding: 5px 20px; border-bottom: 1px solid var(--grid); }
tbody tr.peak td { color: var(--orange-ink); font-weight: 600; }
tbody tr.hover td { background: var(--orange-fill); }
.panel-foot { font-size: 13px; color: var(--muted); min-height: 20px; padding-bottom: 8px; }

.exports { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; align-content: start; grid-row: 1 / 3; }
.export {
  display: flex; align-items: flex-start; gap: 14px; text-align: left; cursor: pointer;
  padding: 16px 18px; border-radius: 12px;
  background: var(--field); border: 1px solid var(--border-strong);
  transition: border-color .15s var(--ease), background-color .15s var(--ease);
}
.export:hover { border-color: var(--olive); }
.export svg { width: 22px; height: 22px; flex: none; fill: none; stroke: var(--olive); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; margin-top: 2px; }
.export span { display: grid; gap: 2px; min-width: 0; }
.export strong { font-weight: 600; }
.export small { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.export.done { background: var(--olive-tint); border-color: var(--olive); }

.note { display: grid; gap: 10px; max-width: 80ch; grid-row: 1 / 3; align-content: start; color: var(--ink-2); }
.note strong { color: var(--ink); }
.note ul { display: flex; flex-wrap: wrap; gap: 6px; }
.note li { padding: 3px 12px; border-radius: 999px; background: #EFE7DA; font-size: 13.5px; color: var(--ink); }

/* ---------- responsive ---------- */
@media (min-width: 1800px) {
  :root { --gutter: 48px; }
  .field { width: 230px; }
}
@media (max-width: 1280px) {
  .ctl { padding: 0 18px; }
  .field { width: 170px; }
}
@media (max-width: 1180px) {
  .ctl { padding: 0 14px; }
  .field { width: 148px; }
  .seg span { padding: 0 14px; }
}
@media (max-width: 1080px) {
  .units { margin-left: 0; width: 100%; padding-left: 0; }
  .results { grid-template-columns: minmax(0, 1fr) 28px minmax(0, 1fr) 28px minmax(0, 1fr); }
  .result { padding: 14px 18px 16px; }
  .value output { font-size: 30px; }
  .result.primary .value output { font-size: 38px; }
  .exports { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .results { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .flow { transform: rotate(90deg); }
}
@media (max-width: 760px) {
  :root { --gutter: 14px; }
  .controls { padding: 14px; gap: 14px; }
  .ctl, .ctl-shape { border-left: 0; padding: 0; width: 100%; }
  .field, .err { width: 100%; max-width: none; }
  .plot, .plot-card { min-height: 360px; }
  .drawer-bar [role="tab"] { padding: 0 10px; font-size: 14px; }
  .meta { flex-wrap: wrap; gap: 8px 24px; }
  .result { padding: 14px 18px; }
  .value output { font-size: 32px; }
  .result.primary .value output { font-size: 40px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
