/* ==========================================================================
   PulleyForge - drafting table UI
   ========================================================================== */
:root {
  --bg: #101820;
  --bg-2: #0B1219;
  --panel: #18242F;
  --panel-2: #1E2C39;
  --line: #2A3945;
  --line-soft: #223140;
  --ink: #DCE7EF;
  --dim: #8CA3B5;
  --faint: #5A7183;
  --brass: #E8A33D;
  --brass-hi: #F4BC64;
  --brass-ink: #171106;
  --scribe: #6FD3E7;
  --pitch: #E36F6F;
  --ok: #6FCF97;
  --err: #E36F6F;
  --grid: rgba(111, 211, 231, 0.055);
  --grid-major: rgba(111, 211, 231, 0.12);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --font-head: "Bahnschrift", "DIN Alternate", "Franklin Gothic Medium", "Segoe UI", system-ui, sans-serif;
  --font-body: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-mono: "Cascadia Mono", "Consolas", "JetBrains Mono", "Menlo", monospace;
}
html[data-theme="light"] {
  --bg: #EDF1F4;
  --bg-2: #E2E8ED;
  --panel: #FFFFFF;
  --panel-2: #F4F7F9;
  --line: #CDD8DF;
  --line-soft: #DCE4E9;
  --ink: #1C2A35;
  --dim: #5A7183;
  --faint: #8CA3B5;
  --brass: #B97A14;
  --brass-hi: #D99A2E;
  --brass-ink: #FFFFFF;
  --scribe: #0E7C97;
  --pitch: #C24B4B;
  --grid: rgba(14, 124, 151, 0.07);
  --grid-major: rgba(14, 124, 151, 0.16);
  --shadow: 0 8px 24px rgba(28, 42, 53, 0.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  background-image: radial-gradient(ellipse 90% 60% at 50% -10%, rgba(111,211,231,0.05), transparent);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 14px;
  min-height: 100vh;
}

/* ---- header ---------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-2);
}
.wordmark {
  font-family: var(--font-head);
  font-size: 21px; letter-spacing: 0.06em; font-weight: 700;
  display: flex; align-items: center; gap: 10px;
}
.wordmark .gear { color: var(--brass); display: inline-flex; }
.wordmark b { color: var(--brass); font-weight: 700; }
.tagline {
  color: var(--faint); font-family: var(--font-mono); font-size: 11.5px;
  letter-spacing: 0.04em; margin-left: 2px;
}
.topbar .spacer { flex: 1; }
.icon-btn {
  background: var(--panel); color: var(--dim);
  border: 1px solid var(--line); border-radius: 8px;
  width: 36px; height: 36px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 16px; transition: color .15s, border-color .15s;
}
.icon-btn:hover { color: var(--ink); border-color: var(--dim); }
.icon-btn:focus-visible, button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--brass); outline-offset: 2px;
}

/* ---- layout ----------------------------------------------------------- */
.workbench {
  display: grid; grid-template-columns: 330px 1fr;
  gap: 18px; padding: 18px 22px; max-width: 1500px; margin: 0 auto;
}
@media (max-width: 900px) {
  .workbench { grid-template-columns: 1fr; }
}

/* ---- control panel ---------------------------------------------------- */
.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.controls { padding: 16px 16px 12px; align-self: start; }
.ctl-section { margin-bottom: 16px; }
.ctl-section:last-child { margin-bottom: 4px; }
.ctl-label {
  display: flex; justify-content: space-between; align-items: baseline;
  font-family: var(--font-head); font-size: 12px; font-weight: 600;
  letter-spacing: 0.09em; text-transform: uppercase; color: var(--dim);
  margin-bottom: 7px;
}
.ctl-label .val {
  font-family: var(--font-mono); font-size: 12.5px; color: var(--brass);
  text-transform: none; letter-spacing: 0;
}
select, input[type="number"], input[type="text"] {
  width: 100%; padding: 9px 10px;
  background: var(--panel-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: 7px;
  font-family: var(--font-mono); font-size: 13.5px;
}
select { cursor: pointer; }
input[type="range"] {
  width: 100%; margin: 6px 0 0; accent-color: var(--brass); cursor: pointer;
}
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row2 .ctl-section { margin-bottom: 0; }

.switch-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 0; border-top: 1px solid var(--line-soft);
}
.switch-row:first-of-type { border-top: none; }
.switch-row .name { color: var(--ink); font-size: 13.5px; }
.switch {
  position: relative; width: 40px; height: 22px; flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch .track {
  position: absolute; inset: 0; border-radius: 22px; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--line); transition: background .15s;
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--dim); transition: transform .18s, background .15s;
}
.switch input:checked + .track { background: var(--brass); border-color: var(--brass); }
.switch input:checked + .track::after { transform: translateX(18px); background: var(--brass-ink); }
.switch input:focus-visible + .track { outline: 2px solid var(--brass); outline-offset: 2px; }

.divider {
  border: none; border-top: 1px dashed var(--line);
  margin: 14px -16px;
}
details.advanced summary {
  cursor: pointer; color: var(--faint); font-family: var(--font-head);
  font-size: 12px; letter-spacing: 0.09em; text-transform: uppercase;
  padding: 4px 0; list-style: none; user-select: none;
}
details.advanced summary::before { content: "+ "; color: var(--brass); }
details.advanced[open] summary::before { content: "− "; }
details.advanced .ctl-section { margin-top: 12px; }
.hint { color: var(--faint); font-size: 11.5px; margin-top: 5px; line-height: 1.45; }
.hint code { font-family: var(--font-mono); color: var(--dim); }

/* ---- buttons ----------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--ink);
  font-family: var(--font-head); font-size: 13px; font-weight: 600;
  letter-spacing: 0.05em; transition: border-color .15s, background .15s, transform .05s;
}
.btn:hover { border-color: var(--dim); }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--brass); border-color: var(--brass); color: var(--brass-ink);
}
.btn.primary:hover { background: var(--brass-hi); border-color: var(--brass-hi); }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }
.export-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 4px;
}
.export-grid .btn.primary { grid-column: 1 / -1; padding: 12px; font-size: 14px; }

/* ---- preview area ------------------------------------------------------ */
.stage { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.viewer { position: relative; overflow: hidden; }
.viewer .canvas-wrap { position: relative; height: 560px; }
@media (max-width: 900px) { .viewer .canvas-wrap { height: 420px; } }
canvas#view2d, #view3d canvas { display: block; }
#view2d, #view3d { position: absolute; inset: 0; }
#view3d { display: none; }
#view2d { cursor: grab; }
#view2d.dragging { cursor: grabbing; }

.view-tabs {
  position: absolute; top: 12px; left: 12px; z-index: 5;
  display: inline-flex; border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden; background: var(--panel);
}
.view-tabs button {
  background: transparent; border: none; color: var(--dim);
  font-family: var(--font-head); font-weight: 600; font-size: 12.5px;
  letter-spacing: 0.07em; padding: 8px 16px; cursor: pointer;
}
.view-tabs button.active { background: var(--brass); color: var(--brass-ink); }

.view-hint {
  position: absolute; bottom: 10px; left: 12px; z-index: 5;
  color: var(--faint); font-family: var(--font-mono); font-size: 10.5px;
  pointer-events: none; letter-spacing: 0.03em;
}
.zoom-cluster {
  position: absolute; top: 12px; right: 12px; z-index: 5; display: flex; gap: 6px;
}

/* ---- stats strip ------------------------------------------------------- */
.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1px; background: var(--line); border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--line);
}
.stat {
  background: var(--panel); padding: 11px 14px;
}
.stat .k {
  font-family: var(--font-head); font-size: 10.5px; font-weight: 600;
  letter-spacing: 0.1em; text-transform: uppercase; color: var(--faint);
}
.stat .v {
  font-family: var(--font-mono); font-size: 16px; color: var(--ink); margin-top: 3px;
}
.stat .v small { font-size: 11px; color: var(--dim); }

/* ---- toasts ------------------------------------------------------------ */
#toasts {
  position: fixed; bottom: 20px; right: 20px; z-index: 100;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-left: 3px solid var(--brass);
  border-radius: 8px; padding: 11px 16px; box-shadow: var(--shadow);
  font-size: 13px; min-width: 220px; max-width: 340px;
  animation: toast-in .22s ease;
}
.toast.error { border-left-color: var(--err); }
.toast.ok { border-left-color: var(--ok); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  * { transition: none !important; }
}

/* ---- footer ------------------------------------------------------------ */
.foot {
  color: var(--faint); font-family: var(--font-mono); font-size: 11px;
  text-align: center; padding: 10px 0 24px; letter-spacing: 0.03em;
}

/* ========================================================================
   Full-screen responsive workspace
   ======================================================================== */
html {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
body {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

.topbar {
  flex: 0 0 auto;
  min-height: 58px;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-left: max(16px, env(safe-area-inset-left));
  z-index: 50;
}

.workbench {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 14px max(14px, env(safe-area-inset-right)) 14px max(14px, env(safe-area-inset-left));
  grid-template-columns: minmax(280px, 330px) minmax(0, 1fr);
  align-items: stretch;
  overflow: hidden;
}

.controls {
  min-height: 0;
  height: 100%;
  align-self: stretch;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.stage {
  min-height: 0;
  height: 100%;
}

.viewer {
  flex: 1 1 auto;
  min-height: 220px;
  display: flex;
  flex-direction: column;
}

.viewer .canvas-wrap {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
}

.stats {
  flex: 0 0 auto;
}

.foot {
  flex: 0 0 auto;
  padding: 5px max(12px, env(safe-area-inset-right)) max(7px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mobile-panel-head,
.controls-toggle,
.drawer-backdrop {
  display: none;
}

#view2d,
#view3d,
#view3d canvas {
  width: 100%;
  height: 100%;
}

#view2d,
#view3d canvas {
  touch-action: none;
}

/* Medium desktops and tablets in landscape */
@media (max-width: 1180px) {
  .workbench {
    grid-template-columns: minmax(270px, 305px) minmax(0, 1fr);
    gap: 12px;
    padding: 12px;
  }
  .controls { padding: 14px 14px 10px; }
  .divider { margin-left: -14px; margin-right: -14px; }
  .stat { padding: 9px 10px; }
  .stat .v { font-size: 14px; }
}

/* Mobile/tablet: full-screen stage with slide-out settings drawer */
@media (max-width: 900px) {
  .topbar {
    min-height: 56px;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: 8px;
    gap: 8px;
  }
  .wordmark { font-size: 18px; gap: 7px; }
  .wordmark .gear svg { width: 20px; height: 20px; }
  .tagline { display: none; }

  .controls-toggle {
    display: inline-flex;
    width: auto;
    min-width: 44px;
    padding: 0 11px;
    gap: 7px;
  }
  .controls-toggle .controls-text {
    font-family: var(--font-head);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: .04em;
  }

  .workbench {
    display: block;
    min-height: 0;
    padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    overflow: hidden;
  }

  .stage {
    width: 100%;
    height: 100%;
    gap: 8px;
  }

  .viewer { min-height: 0; }

  .controls {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 80;
    width: min(380px, calc(100vw - 34px));
    height: 100dvh;
    max-height: none;
    padding: 0 16px max(20px, env(safe-area-inset-bottom));
    border-radius: 0 var(--radius) var(--radius) 0;
    transform: translateX(calc(-100% - 18px));
    transition: transform .2s ease;
    box-shadow: 16px 0 36px rgba(0,0,0,.5);
    visibility: hidden;
  }

  body.controls-open .controls {
    transform: translateX(0);
    visibility: visible;
  }

  .mobile-panel-head {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 58px;
    margin: 0 -16px 14px;
    padding: max(8px, env(safe-area-inset-top)) 12px 8px max(16px, env(safe-area-inset-left));
    background: var(--panel);
    border-bottom: 1px solid var(--line);
  }
  .mobile-panel-head strong {
    font-family: var(--font-head);
    font-size: 15px;
    letter-spacing: .06em;
    text-transform: uppercase;
  }

  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: block;
    background: rgba(0,0,0,.55);
    backdrop-filter: blur(2px);
  }
  .drawer-backdrop[hidden] { display: none; }

  .stats {
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
  }
  .stat {
    flex: 0 0 min(145px, 40vw);
    scroll-snap-align: start;
  }

  .foot { display: none; }

  .icon-btn,
  .btn,
  select,
  input[type="number"],
  input[type="text"] {
    min-height: 44px;
  }

  .view-tabs { top: 8px; left: 8px; }
  .view-tabs button { min-height: 42px; padding: 8px 12px; }
  .zoom-cluster { top: 8px; right: 8px; }
  .view-hint { left: 9px; bottom: 7px; }
  #toasts {
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    left: max(10px, env(safe-area-inset-left));
  }
  .toast { width: 100%; min-width: 0; max-width: none; }
}

@media (max-width: 520px) {
  .topbar > .icon-btn:not(.controls-toggle) { width: 42px; height: 42px; }
  .controls-toggle .controls-text { display: none; }
  .controls-toggle { width: 42px; padding: 0; }
  .row2 { grid-template-columns: 1fr; gap: 0; }
  .row2 .ctl-section { margin-bottom: 14px; }
  .export-grid { grid-template-columns: repeat(3, 1fr); }
  .export-grid .btn.primary { grid-column: auto; }
  .export-grid .btn { padding-left: 7px; padding-right: 7px; letter-spacing: .02em; }
  .view-tabs button { font-size: 11px; padding: 7px 9px; }
  .zoom-cluster { top: 58px; flex-direction: column; }
  .zoom-cluster .icon-btn { width: 40px; height: 40px; }
}

/* Short landscape screens: preserve maximum drawing area. */
@media (max-height: 620px) and (min-width: 901px) {
  .topbar { min-height: 50px; padding-top: 7px; padding-bottom: 7px; }
  .workbench { padding-top: 8px; padding-bottom: 8px; }
  .stage { gap: 8px; }
  .foot { display: none; }
  .stat { padding-top: 7px; padding-bottom: 7px; }
  .stat .k { font-size: 9.5px; }
  .stat .v { font-size: 13px; }
}

@media (max-height: 500px) and (max-width: 900px) {
  .stats { display: none; }
  .view-hint { display: none; }
}
