:root {
  --bg: #f2f4f3;
  --card: #ffffff;
  --ink: #17201c;
  --muted: #64716b;
  --line: #dde3e0;
  --accent: #0b7a52;
  --accent-ink: #ffffff;
  --accent-soft: #e3f3ec;
  --warn: #c76a00;
  --bad: #c62828;
  --good: #1b8a3a;
  --cut: #d64545;
  --fill: #2f6fd6;
  --field: #f6f8f7;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1412; --card: #18201d; --ink: #e8efeb; --muted: #94a39c; --line: #2a3531;
    --accent: #2fb983; --accent-ink: #04140d; --accent-soft: #163a2c; --field: #111917;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
#bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 10px;
  background: var(--accent); color: var(--accent-ink);
}
@media (prefers-color-scheme: dark) { #bar { background: #0b5c40; color: #fff; } }
#titles { flex: 1; min-width: 0; padding-left: 4px; }
#title { margin: 0; font-size: 19px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#subtitle { font-size: 12.5px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 12px; background: rgba(255,255,255,.14); color: inherit;
  display: grid; place-items: center; flex: none; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; }

main { max-width: 1100px; margin: 0 auto; padding: 14px 14px 40px; }

/* ---------- home ---------- */
.hero { padding: 6px 2px 12px; }
.hero h2 { margin: 0 0 2px; font-size: 22px; }
.hero p { margin: 0; color: var(--muted); font-size: 14px; }
.tools { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tool {
  display: flex; flex-direction: column; gap: 8px; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px;
  box-shadow: var(--shadow); cursor: pointer; min-height: 118px;
}
.tool:active { transform: scale(.98); }
.tool .ic { width: 40px; height: 40px; border-radius: 11px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.tool .ic svg { width: 24px; height: 24px; }
.tool b { font-size: 15.5px; }
.tool span { font-size: 12.5px; color: var(--muted); line-height: 1.3; }
.credit { margin: 26px auto 0; text-align: center; color: var(--muted); font-size: 12px; }
.credit img { display: block; width: 118px; height: auto; margin: 0 auto 6px; border-radius: 9px; }
.credit a { color: inherit; white-space: nowrap; }

/* ---------- cards & fields ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card > h3 { margin: 0 0 2px; font-size: 16px; }
.card > .sub { margin: 0 0 10px; color: var(--muted); font-size: 12.5px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.fld { display: flex; flex-direction: column; gap: 4px; flex: 1 1 130px; min-width: 0; }
.fld > span { font-size: 12px; color: var(--muted); }
.fld input, .fld select, .fld textarea {
  width: 100%; padding: 10px 11px; border: 1px solid var(--line); border-radius: 10px; background: var(--field);
  font-size: 16px; /* 16px stops iOS zooming on focus */
}
.fld input { font-family: var(--mono); font-size: 15.5px; }
.fld input:focus, .fld select:focus, .grid input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.seg { display: flex; background: var(--field); border: 1px solid var(--line); border-radius: 11px; padding: 3px; gap: 3px; flex: 1 1 100%; }
.seg button { flex: 1; border: 0; border-radius: 8px; padding: 8px 6px; background: transparent; font-size: 14px; cursor: pointer; }
.seg button.on { background: var(--card); color: var(--accent); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.hint { font-size: 12.5px; color: var(--muted); margin: 8px 0 0; }
.warn { color: var(--warn); font-size: 13.5px; margin: 8px 0 0; }
.warn::before { content: "⚠︎ "; }

.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 13px; font-size: 14.5px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.small { padding: 7px 10px; font-size: 13.5px; }
.btn.danger { color: var(--bad); }
.btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

/* ---------- input grid ---------- */
.grid-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; -webkit-overflow-scrolling: touch; }
table.grid { border-collapse: collapse; width: 100%; min-width: max-content; }
.grid th { font-size: 11.5px; font-weight: 600; color: var(--muted); text-align: left; padding: 7px 6px; background: var(--field); border-bottom: 1px solid var(--line); white-space: nowrap; }
.grid td { padding: 0; border-bottom: 1px solid var(--line); }
.grid tr:last-child td { border-bottom: 0; }
.grid td.n { width: 30px; text-align: center; color: var(--muted); font-size: 12px; }
.grid input { width: 100%; min-width: 60px; border: 0; background: transparent; padding: 10px 7px; font-family: var(--mono); font-size: 15px; }
.grid input.txt { font-family: inherit; }
.grid td.del { width: 34px; }
.grid td.del button { border: 0; background: none; color: var(--muted); font-size: 18px; width: 34px; height: 40px; cursor: pointer; }

/* ---------- results ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.stat { background: var(--field); border-radius: 11px; padding: 9px 11px; min-width: 0; }
.stat small { display: block; font-size: 11.5px; color: var(--muted); }
.stat b { font-size: 18px; font-weight: 650; word-break: break-word; }
.stat i { font-style: normal; font-size: 12px; color: var(--muted); margin-left: 3px; }
.stat.accent b { color: var(--accent); } .stat.good b { color: var(--good); } .stat.bad b { color: var(--bad); }
.stat.warnc b { color: var(--warn); } .stat.cut b { color: var(--cut); } .stat.fill b { color: var(--fill); }
.res-head { display: flex; align-items: center; gap: 8px; margin: 4px 0 8px; }
.res-head h3 { flex: 1; margin: 0; font-size: 15.5px; }
table.res { border-collapse: collapse; min-width: 100%; font-family: var(--mono); font-size: 13px; }
.res th { font-family: -apple-system, sans-serif; font-size: 11.5px; color: var(--muted); font-weight: 600; text-align: right; padding: 6px 8px; background: var(--field); white-space: nowrap; }
.res td { text-align: right; padding: 6px 8px; border-top: 1px solid var(--line); white-space: nowrap; }
.res th:first-child, .res td:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
.res th:first-child { background: var(--field); }
.res tr.total td { font-weight: 700; }

/* ---------- drawings ---------- */
.sketch { width: 100%; aspect-ratio: 4 / 3; background: var(--field); border: 1px solid var(--line); border-radius: 12px; display: block; touch-action: manipulation; }
.sketch.tall { aspect-ratio: 1 / 1; }
.sketch-wrap { position: relative; }
.sketch-wrap .zoom { position: absolute; right: 8px; bottom: 8px; }
.plan-wrap { overflow: auto; border-radius: 10px; background: #8a918e; padding: 10px; -webkit-overflow-scrolling: touch; }
.plan-wrap svg { display: block; width: 100%; height: auto; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.3); }

/* ---------- menu sheet, full screen viewer, toast ---------- */
#sheet { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; justify-content: center; }
#sheet .panel {
  width: 100%; max-width: 560px; background: var(--card); border-radius: 18px 18px 0 0; padding: 10px 14px calc(env(safe-area-inset-bottom) + 16px);
  max-height: 88vh; overflow: auto;
}
#sheet .panel h3 { margin: 6px 4px 10px; font-size: 16px; }
#sheet .item { display: flex; width: 100%; gap: 12px; align-items: center; text-align: left; border: 0; background: none; padding: 13px 6px; border-top: 1px solid var(--line); font-size: 16px; cursor: pointer; }
#sheet .item small { display: block; color: var(--muted); font-size: 12.5px; }
#sheet.full { align-items: stretch; background: #000; }
#sheet.full .viewer { flex: 1; display: flex; flex-direction: column; background: var(--bg); }
#sheet.full .viewer header { display: flex; align-items: center; gap: 8px; padding: calc(env(safe-area-inset-top) + 8px) 12px 8px; background: var(--accent); color: var(--accent-ink); }
#sheet.full .viewer header b { flex: 1; }
#sheet.full .viewer .scroll { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y pinch-zoom; }
#sheet.full .viewer svg { display: block; width: 100%; height: auto; background: #fff; }
#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 22px); transform: translateX(-50%) translateY(20px);
  background: #1c2522; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 60; max-width: 90vw;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

@media (min-width: 900px) {
  .two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; align-items: start; }
}

/* ---------- print: only the plan sheet ---------- */
@media print {
  @page { size: A4 landscape; margin: 0; }
  body { background: #fff; padding: 0; }
  #bar, #toast, #sheet, main > *:not(.print-only) { display: none !important; }
  main { padding: 0; max-width: none; }
  .print-only { display: block !important; }
  .print-only svg { width: 100%; height: auto; }
}
.print-only { display: none; }
