:root {
  --ink: #1f2933;
  --muted: #5f6b76;
  --line: #e3e7eb;
  --bg: #f6f7f8;
  --go: #16a34a;
  --go-dark: #12813b;
  --danger: #dc2626;
  --sel: #f59e0b;
  --top-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); overscroll-behavior: none; }
body { display: flex; flex-direction: column; height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
svg { display: block; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  flex: 0 0 auto; height: var(--top-h); display: flex; align-items: center; gap: 8px;
  padding: 0 10px; padding-top: env(safe-area-inset-top, 0px); height: calc(var(--top-h) + env(safe-area-inset-top, 0px));
  background: #fff; border-bottom: 1px solid var(--line); z-index: 5;
}
.title { display: flex; flex-direction: column; line-height: 1.15; margin-right: auto; min-width: 0; }
.title strong { font-size: 17px; }
.title span { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { min-width: 0; overflow: hidden; display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); padding: 6px 10px; border-radius: 999px; background: var(--bg); white-space: nowrap; }
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: #9aa5b1; transition: background .3s; }
.status.ok .dot { background: var(--go); }
.status.busy .dot { background: var(--sel); }
.status.err .dot { background: var(--danger); }
.status.err { color: var(--danger); }
.icon-btn {
  display: flex; align-items: center; gap: 6px; height: 42px; min-width: 42px; padding: 0 12px;
  border: 1px solid var(--line); background: #fff; border-radius: 12px; font-size: 15px; font-weight: 600;
}
.icon-btn:active { background: var(--bg); }
.icon-btn svg, .btn svg, .zoom svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: .35; }

/* canvas */
.stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; background: #fff; }
#canvas { width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; cursor: grab; }
.stage.drawing #canvas { cursor: crosshair; }

.zoom { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 8px; z-index: 3; }
.zoom button { width: 46px; height: 46px; border-radius: 14px; border: 1px solid var(--line); background: rgba(255,255,255,.95); box-shadow: 0 2px 8px rgba(0,0,0,.08); display: grid; place-items: center; }
.zoom button:active { background: var(--bg); }

/* instruction banner while drawing */
.banner {
  position: absolute; left: 10px; right: 66px; top: 10px; z-index: 4;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 12px; border-radius: 16px; background: rgba(255,255,255,.97);
  box-shadow: 0 6px 24px rgba(0,0,0,.14); border: 2px solid var(--accent, #1e88e5);
  animation: drop .22s ease-out;
}
.banner-text { flex: 1 1 220px; font-size: 16px; line-height: 1.3; font-weight: 600; }
.banner-text small { display: block; font-weight: 500; color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.banner-actions { display: flex; gap: 8px; margin-left: auto; }
@keyframes drop { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 48px; padding: 0 18px; border-radius: 14px; border: 0; font-weight: 700; font-size: 17px; }
.btn-go { background: var(--go); color: #fff; }
.btn-go:active { background: var(--go-dark); }
.btn-ghost { background: var(--bg); border: 1px solid var(--line); }
.btn-danger { background: #fff; color: var(--danger); border: 2px solid #f5c2c2; }
.btn-big { height: 56px; font-size: 19px; padding: 0 26px; }
.btn:disabled { opacity: .45; }

/* toolbar */
.toolbar {
  flex: 0 0 auto; display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px;
  padding: 10px; padding-bottom: calc(10px + var(--safe-b)); background: #fff; border-top: 1px solid var(--line); z-index: 5;
}
.tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 74px; padding: 8px 4px; border-radius: 16px; border: 2px solid var(--line); background: #fff;
  text-align: center; transition: transform .12s, border-color .15s, background .15s;
}
.tool:active { transform: scale(.97); }
.tool .ico { width: 44px; height: 26px; }
.tool b { font-size: 14.5px; line-height: 1.1; }
.tool small { font-size: 11.5px; color: var(--muted); line-height: 1.1; }
.tool.active { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, #fff); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 25%, transparent); }

@media (max-width: 900px) {
  .toolbar { grid-template-columns: repeat(4, 1fr); gap: 7px; padding: 8px; padding-bottom: calc(8px + var(--safe-b)); }
  .tool { min-height: 64px; padding: 6px 2px; border-radius: 14px; }
  .tool small { display: none; }
}
@media (max-width: 520px) {
  .icon-btn span { display: none; }
  .icon-btn { padding: 0 9px; }
  .title span { display: none; }
  .status { padding: 6px 8px; }
  .tool b { font-size: 13px; }
  .tool .ico { width: 38px; height: 22px; }
  .banner { right: 64px; }
  .banner-text { font-size: 15px; }
  .btn { height: 46px; padding: 0 14px; font-size: 16px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .toolbar { grid-template-columns: repeat(8, 1fr); }
  .tool { min-height: 50px; }
  .tool .ico { height: 18px; }
}

/* bottom sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 32, .35); z-index: 20; animation: fade .2s; }
.sheet {
  position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); z-index: 21;
  width: min(560px, 100%); max-height: 88dvh; display: flex; flex-direction: column;
  background: #fff; border-radius: 22px 22px 0 0; box-shadow: 0 -10px 40px rgba(0,0,0,.18);
  padding: 16px 16px calc(14px + var(--safe-b)); animation: rise .24s ease-out;
}
@media (min-width: 700px) { .sheet { bottom: 24px; border-radius: 22px; } }
@keyframes rise { from { transform: translate(-50%, 30px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
@keyframes fade { from { opacity: 0; } }
.sheet-head { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.sheet-head h2 { margin: 0; font-size: 21px; }
.swatch { width: 40px; height: 22px; flex: 0 0 auto; }
.sheet-body { overflow-y: auto; padding: 4px 2px 8px; }
.field { margin: 14px 0 0; }
.field > label, .field > .lbl { display: block; font-weight: 700; font-size: 16px; margin-bottom: 8px; }
.field > .lbl small, .field > label small { font-weight: 500; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { min-width: 58px; height: 46px; padding: 0 14px; border-radius: 12px; border: 2px solid var(--line); background: #fff; font-weight: 700; font-size: 16px; }
.chip.on { border-color: var(--go); background: #ecfdf3; color: var(--go-dark); }
.inp-row { display: flex; align-items: center; gap: 10px; }
.inp { width: 100%; height: 50px; border: 2px solid var(--line); border-radius: 12px; padding: 0 14px; font: inherit; font-size: 18px; background: #fff; color: var(--ink); }
.inp:focus, textarea.inp:focus { outline: none; border-color: #60a5fa; }
.inp-unit { font-weight: 700; color: var(--muted); font-size: 17px; }
.inp-row .inp { max-width: 180px; }
textarea.inp { height: auto; min-height: 92px; padding: 12px 14px; resize: vertical; line-height: 1.35; }
.sheet-actions { display: flex; gap: 10px; justify-content: space-between; padding-top: 12px; border-top: 1px solid var(--line); }
.sheet-actions .btn-go { flex: 1 1 auto; }
.hint { font-size: 14px; color: var(--muted); margin: 6px 0 0; }
.kinds { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.kind { display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 8px 12px; border-radius: 14px; border: 2px solid var(--line); background: #fff; text-align: left; font-weight: 700; font-size: 15.5px; }
.kind.on { border-color: var(--go); background: #ecfdf3; }
.kind svg { width: 30px; height: 30px; flex: 0 0 auto; }

/* help */
.help, .locked { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: rgba(15,23,32,.45); padding: 16px; animation: fade .2s; }
.help-card { width: min(560px, 100%); max-height: 92dvh; overflow-y: auto; background: #fff; border-radius: 22px; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.help-card h2 { margin: 0 0 10px; font-size: 22px; line-height: 1.25; }
.help-card ol { padding-left: 22px; margin: 10px 0 12px; font-size: 17px; line-height: 1.45; }
.help-card li { margin-bottom: 8px; }
.help-tips { font-size: 15px; color: var(--muted); line-height: 1.5; margin: 0 0 16px; }
.help-card .btn { width: 100%; }

/* menu */
.menu { position: fixed; top: calc(var(--top-h) + 8px + env(safe-area-inset-top, 0px)); right: 10px; z-index: 25; display: flex; flex-direction: column; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.15); overflow: hidden; }
.menu button { border: 0; background: #fff; text-align: left; padding: 14px 18px; font-size: 15px; font-weight: 600; border-bottom: 1px solid var(--line); }
.menu button:active { background: var(--bg); }
.menu .menu-close { color: var(--muted); border-bottom: 0; }

.toast { position: fixed; left: 50%; bottom: calc(170px + var(--safe-b)); transform: translateX(-50%); z-index: 40; background: #1f2933; color: #fff; padding: 12px 18px; border-radius: 14px; font-weight: 600; font-size: 15px; max-width: calc(100% - 32px); text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.25); animation: rise2 .2s ease-out; }
@keyframes rise2 { from { transform: translate(-50%, 10px); opacity: 0; } }

/* drawn items */
.it-hit { stroke: transparent; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.it-case { fill: none; stroke: #fff; stroke-linecap: round; stroke-linejoin: round; }
.it-line { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.it-sel .it-case { stroke: #fde68a; }
.it-label-bg { fill: #fff; stroke-linejoin: round; }
.it-label { font-family: system-ui, sans-serif; font-weight: 700; paint-order: stroke; }
.draft-line { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.draft-pt { fill: #fff; stroke-width: 3; }
.snap-ring { fill: none; stroke: var(--go); }

/* notches in landscape, very small phones */
.topbar, .toolbar { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
.banner { left: max(10px, env(safe-area-inset-left)); right: calc(56px + max(10px, env(safe-area-inset-right))); }
.zoom { right: max(10px, env(safe-area-inset-right)); }
.status .txt { overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 520px) {
  .banner-text small { display: none; }
  .banner-text { flex-basis: 100%; font-size: 14.5px; }
  .banner { padding: 8px 10px; gap: 8px; }
}
@media (max-width: 380px) {
  .status .txt { display: none; }
  .topbar { gap: 6px; }
  .icon-btn { min-width: 40px; height: 40px; }
}

/* send button + dialog */
.send-btn {
  display: flex; align-items: center; gap: 8px; height: 42px; padding: 0 16px; border: 0; border-radius: 12px;
  background: var(--go); color: #fff; font-size: 15.5px; font-weight: 800; white-space: nowrap;
}
.send-btn:active { background: var(--go-dark); }
.send-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.3; stroke-linecap: round; stroke-linejoin: round; }
.send-actions { display: flex; gap: 10px; margin-top: 16px; }
.send-actions .btn-go { flex: 1 1 auto; width: auto; }
.help-card .send-actions .btn { width: auto; }
.done-check { width: 72px; height: 72px; border-radius: 50%; background: #dcfce7; display: grid; place-items: center; margin: 4px auto 12px; }
.done-check svg { width: 40px; height: 40px; fill: none; stroke: var(--go); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
#send-done { text-align: center; }
.menu-info { padding: 12px 18px; font-size: 13.5px; color: var(--muted); border-bottom: 1px solid var(--line); background: var(--bg); max-width: 300px; line-height: 1.4; }
.menu a { display: block; padding: 10px 18px; font-size: 14px; font-weight: 600; color: var(--ink); text-decoration: none; border-bottom: 1px solid var(--line); }
@media (max-width: 420px) {
  .title { display: none; }
  .status { margin-right: auto; }
  .send-btn { padding: 0 12px; font-size: 15px; }
}
