:root {
  color-scheme: light dark;
  --bg: #eef1f4;
  --bg-grid: rgba(31, 41, 55, 0.05);
  --panel: #ffffff;
  --panel-alt: #f5f7f9;
  --panel-inset: #f0f3f6;
  --line: #e1e6ec;
  --line-strong: #c7cfd8;
  --text: #161c24;
  --muted: #66727f;
  --faint: #93a0ad;
  --accent: #2f6bff;
  --accent-dark: #1f4fd6;
  --accent-soft: rgba(47, 107, 255, 0.1);
  --accent-ring: rgba(47, 107, 255, 0.28);
  --ok: #117a3a;
  --ok-soft: #e7f6ec;
  --ok-line: #aadcb9;
  --warn: #a45a06;
  --warn-soft: #fdf3e1;
  --warn-line: #efce85;
  --danger: #b42318;
  --radius: 9px;
  --radius-sm: 7px;
  --shadow-sm: 0 1px 2px rgba(20, 28, 38, 0.05);
  --shadow-md: 0 8px 24px rgba(20, 28, 38, 0.08);
  --shadow-lg: 0 18px 45px rgba(20, 28, 38, 0.1);
  --ease: cubic-bezier(0.2, 0.6, 0.2, 1);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --bg-grid: rgba(255, 255, 255, 0.04);
    --panel: #181d25;
    --panel-alt: #1f2630;
    --panel-inset: #131820;
    --line: #2a323d;
    --line-strong: #3a4350;
    --text: #eaeef3;
    --muted: #97a3b1;
    --faint: #6c7785;
    --accent: #5b8bff;
    --accent-dark: #769fff;
    --accent-soft: rgba(91, 139, 255, 0.16);
    --accent-ring: rgba(91, 139, 255, 0.4);
    --ok: #59d089;
    --ok-soft: rgba(43, 122, 73, 0.22);
    --ok-line: rgba(89, 208, 137, 0.4);
    --warn: #f0b65f;
    --warn-soft: rgba(140, 92, 20, 0.22);
    --warn-line: rgba(240, 182, 95, 0.4);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 18px 45px rgba(0, 0, 0, 0.5);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow: hidden;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select {
  font: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

::selection {
  background: var(--accent-soft);
}

.app-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(16px);
  z-index: 5;
}

.brand,
.toolstrip,
.header-actions,
.header-export {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions {
  min-width: 0;
}

.header-export {
  gap: 8px;
  padding-left: 14px;
  border-left: 1px solid var(--line);
}

.brand h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.01em;
}

.icon-button,
.viewport-tool,
.tool-button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  height: 40px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease),
    color 0.15s var(--ease), box-shadow 0.15s var(--ease), transform 0.06s var(--ease);
}

.icon-button:hover,
.viewport-tool:hover,
.tool-button:hover {
  border-color: var(--line-strong);
  background: var(--panel-alt);
}

.icon-button:active,
.viewport-tool:active,
.tool-button:active {
  transform: translateY(1px);
}

.icon-button {
  width: 38px;
  display: grid;
  place-items: center;
  gap: 3px;
  padding: 9px;
}

.icon-button span {
  display: block;
  width: 17px;
  height: 2px;
  border-radius: 2px;
  background: var(--text);
  transition: background 0.15s var(--ease);
}

.tool-button,
.viewport-tool {
  padding: 0 14px;
  font-weight: 600;
}

.tool-button.active,
.viewport-tool.active {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  color: var(--accent-dark);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 20%, transparent);
}

/* ---------- Workspace grid ---------- */
.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 360px minmax(560px, 1fr) 360px;
  overflow: hidden;
  border-bottom: 1px solid var(--line);
}

.panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.panel::-webkit-scrollbar {
  width: 10px;
}

.panel::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border-radius: 999px;
  border: 3px solid var(--panel);
}

.export-panel {
  border-right: 0;
  border-left: 1px solid var(--line);
}

.export-panel section + section {
  border-top: 1px solid var(--line);
}

.panel section,
.parameter-panel {
  padding: 18px 20px;
}

.export-panel section {
  padding: 14px 18px;
}

.parameter-panel {
  padding-top: 0;
}

.panel-heading {
  position: sticky;
  top: 0;
  z-index: 2;
  height: 58px;
  margin: 0 -20px 14px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(8px);
}

h2 {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--muted);
}

.panel-heading h2 {
  font-size: 15px;
  text-transform: none;
  letter-spacing: -0.01em;
  color: var(--text);
}

/* ---------- Status pill ---------- */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 12px 0 10px;
  border-radius: 999px;
  border: 1px solid var(--ok-line);
  background: var(--ok-soft);
  color: var(--ok);
  font-size: 12.5px;
  font-weight: 700;
}

.status-pill::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 0 3px color-mix(in srgb, currentColor 22%, transparent);
}

/* ---------- Parameter controls ---------- */
.unit-readout,
.mode-toggle,
.control {
  display: grid;
  gap: 10px;
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}

.control:last-of-type {
  border-bottom: 0;
}

.unit-readout span,
.control-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13.5px;
  font-weight: 600;
}

.unit-readout {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.unit-readout strong {
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel-inset);
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
}

.mode-toggle {
  grid-template-columns: repeat(2, 1fr);
  gap: 0;
  margin: 0;
  padding-bottom: 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
}

.mode-toggle legend {
  grid-column: 1 / -1;
  padding: 0 0 10px;
  font-size: 13.5px;
  font-weight: 600;
}

.mode-toggle label {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 1px solid var(--line);
  background: var(--panel);
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s var(--ease), color 0.15s var(--ease), border-color 0.15s var(--ease);
}

.mode-toggle label:first-of-type {
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
}

.mode-toggle label:last-of-type {
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  border-left: 0;
}

.mode-toggle label:hover {
  color: var(--text);
}

.mode-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-toggle label:has(input:checked) {
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
  background: var(--accent-soft);
  color: var(--accent-dark);
  z-index: 1;
}

.mode-toggle label:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  z-index: 2;
}

select,
.number-field {
  height: 42px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--text);
  padding: 0 12px;
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

select:focus,
.number-field:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.range-row {
  display: grid;
  grid-template-columns: 1fr 84px 32px;
  align-items: center;
  gap: 12px;
}

/* Custom range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 22px;
  background: transparent;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: var(--panel-inset);
  border: 1px solid var(--line);
}

input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 999px;
  background: var(--panel-inset);
  border: 1px solid var(--line);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  margin-top: -7px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
  box-shadow: var(--shadow-sm), 0 0 0 1px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: transform 0.1s var(--ease), box-shadow 0.15s var(--ease);
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
  box-shadow: var(--shadow-sm);
}

input[type="range"]:hover::-webkit-slider-thumb {
  transform: scale(1.12);
}

input[type="range"]:active::-webkit-slider-thumb {
  transform: scale(1.05);
  box-shadow: 0 0 0 6px var(--accent-soft);
}

input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 4px var(--accent-ring);
}

.number-field {
  width: 84px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.number-field::-webkit-outer-spin-button,
.number-field::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.unit-label,
.range-limits {
  color: var(--faint);
  font-size: 12.5px;
}

.range-limits {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-width: 56px;
  justify-content: flex-end;
  color: var(--faint);
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.control-label > span:first-child {
  color: var(--text);
}

/* ---------- Buttons ---------- */
.primary-button,
.secondary-button,
.reset-button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: var(--panel);
  color: var(--text);
  font-weight: 600;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease),
    box-shadow 0.15s var(--ease), transform 0.06s var(--ease);
}

.secondary-button:hover,
.reset-button:hover {
  border-color: var(--line-strong);
  background: var(--panel-alt);
}

.primary-button:active,
.secondary-button:active,
.reset-button:active {
  transform: translateY(1px);
}

.primary-button {
  width: 100%;
  min-height: 46px;
  border-color: var(--accent);
  background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 94%, white), var(--accent));
  color: #fff;
  box-shadow: 0 8px 18px color-mix(in srgb, var(--accent) 30%, transparent);
}

.primary-button:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

.primary-button.compact,
.secondary-button.compact {
  width: auto;
  min-width: 78px;
  min-height: 40px;
  padding: 0 16px;
  box-shadow: none;
}

.primary-button.compact {
  box-shadow: 0 6px 14px color-mix(in srgb, var(--accent) 28%, transparent);
}

.secondary-button.compact {
  background: var(--panel);
  color: var(--text);
}

.secondary-button.wide,
.reset-button {
  width: 100%;
}

.reset-button {
  margin-top: 18px;
  color: var(--muted);
}

/* ---------- Viewport ---------- */
.viewport-wrap {
  position: relative;
  min-width: 0;
  overflow: hidden;
  background:
    radial-gradient(120% 120% at 50% 0%, color-mix(in srgb, var(--accent) 5%, transparent), transparent 55%),
    linear-gradient(var(--bg-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px),
    var(--bg);
  background-size: auto, 24px 24px, 24px 24px, auto;
}

.viewport-toolbar {
  position: absolute;
  z-index: 2;
  top: 14px;
  left: 18px;
  display: flex;
  gap: 6px;
  padding: 5px;
  border-radius: 11px;
  background: color-mix(in srgb, var(--panel) 82%, transparent);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(10px);
}

.viewport-toolbar .viewport-tool {
  height: 34px;
  border: 1px solid transparent;
  background: transparent;
  font-size: 13.5px;
}

.viewport-toolbar .viewport-tool:hover {
  background: var(--panel-alt);
  border-color: var(--line);
}

.viewport-toolbar .viewport-tool.active {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}

#previewCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

#previewCanvas:active {
  cursor: grabbing;
}

.axis-widget {
  position: absolute;
  left: 28px;
  bottom: 42px;
  width: 88px;
  height: 88px;
  pointer-events: none;
}

.axis {
  position: absolute;
  font-size: 13px;
  font-weight: 800;
}

.axis.x { color: #e23b2e; left: 54px; bottom: 4px; }
.axis.y { color: #16a34a; left: 66px; top: 32px; }
.axis.z { color: var(--accent); left: 24px; top: 0; }

.axis-widget::before,
.axis-widget::after {
  content: "";
  position: absolute;
  left: 30px;
  bottom: 22px;
  width: 56px;
  height: 2px;
  border-radius: 2px;
  background: #e23b2e;
  transform: rotate(25deg);
  transform-origin: left center;
}

.axis-widget::after {
  width: 54px;
  background: #16a34a;
  transform: rotate(-37deg);
}

.viewport-caption {
  position: absolute;
  right: 16px;
  bottom: 14px;
  margin: 0;
  padding: 5px 11px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  color: var(--muted);
  font-size: 12.5px;
}

/* ---------- Info lists ---------- */
.info-list {
  margin: 0;
  display: grid;
  gap: 9px;
}

.info-list div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--line);
}

.info-list div:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.info-list dt {
  color: var(--muted);
  font-size: 13px;
}

.info-list dd {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.compact {
  gap: 8px;
}

/* ---------- Validation card ---------- */
.validation-card {
  margin: 10px 16px 16px;
  padding: 12px 14px;
  border: 1px solid var(--ok-line);
  background: var(--ok-soft);
  color: var(--ok);
  border-radius: var(--radius-sm);
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: start;
  column-gap: 10px;
  row-gap: 2px;
}

.validation-card::before {
  content: "✓";
  grid-row: 1 / span 2;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: currentColor;
  color: var(--ok-soft);
  font-size: 13px;
  font-weight: 800;
}

.validation-card.invalid {
  border-color: var(--warn-line);
  background: var(--warn-soft);
  color: var(--warn);
}

.validation-card.invalid::before {
  content: "!";
}

.validation-card span {
  font-size: 13px;
  color: inherit;
  opacity: 0.9;
}

/* ---------- Drawings ---------- */
.drawing-panel {
  background: var(--panel-alt);
}

.drawing-grid {
  display: grid;
  gap: 12px;
}

.drawing-grid figure {
  margin: 0;
  display: grid;
  gap: 6px;
}

.drawing-grid figcaption {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 700;
}

.drawing-grid figcaption::before {
  content: "";
  width: 10px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
}

.drawing-canvas {
  display: block;
  width: 100%;
  height: 108px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  box-shadow: var(--shadow-sm);
}

#frontViewCanvas {
  height: 144px;
}

.summary-panel,
.stats-panel {
  padding-top: 14px;
  padding-bottom: 14px;
}

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .topbar {
    height: auto;
    min-height: 64px;
    flex-wrap: wrap;
    padding: 12px;
  }

  .header-actions {
    width: 100%;
    justify-content: space-between;
  }

  .workspace {
    grid-template-columns: 1fr;
    overflow: auto;
  }

  .panel,
  .export-panel {
    border: 0;
    border-bottom: 1px solid var(--line);
    max-height: none;
  }

  .panel-heading {
    position: static;
  }

  .viewport-wrap {
    min-height: 520px;
  }
}

@media (max-width: 560px) {
  .brand h1 {
    font-size: 16px;
  }

  .toolstrip {
    max-width: 100%;
    overflow-x: auto;
  }

  .header-actions,
  .header-export {
    width: 100%;
  }

  .header-actions {
    flex-wrap: wrap;
  }

  .header-export {
    padding-left: 0;
    border-left: 0;
  }

  .primary-button.compact,
  .secondary-button.compact {
    flex: 1;
  }

  .range-row {
    grid-template-columns: 1fr 70px 24px;
  }

  .viewport-toolbar {
    left: 10px;
    right: 10px;
    overflow-x: auto;
  }

  .viewport-tool {
    padding: 0 11px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
