:root {
  --border: #e0e0e0;
  --bg: #f8f6f2;
  --panel-bg: #ffffff;
  --text: #0f0f0f;
  --text-sub: #6b6b6b;
  --accent: #f28c06;
  --accent-dark: #d97a00;
  --accent-soft: #fff8f0;
  --accent-text: #b35f00;
  --error: #e53935;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

* {
  box-sizing: border-box;
}

/* Components below set display:flex, which would otherwise override [hidden]. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border);
}

.app-header-top {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.hub-link {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-sub);
  text-decoration: none;
}

.hub-link:hover {
  color: var(--accent-text);
  text-decoration: underline;
}

.app-header h1 {
  font-size: 1.1rem;
  margin: 0;
}

.mode-switch {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 4px;
}

.mode-btn {
  white-space: nowrap;
  border: none;
  background: transparent;
  min-height: 40px;
  padding: 8px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.95rem;
}

.mode-btn.active {
  background: var(--accent-dark);
  color: #fff;
}

.app-main {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
  align-items: start;
}

/* Both columns stay in view while the page scrolls; the panel scrolls inside. */
.panel,
.stage {
  position: sticky;
  top: 16px;
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.shape-fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.shape-fieldset legend {
  padding: 0 6px;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-sub);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
}

.field input,
.field select {
  min-height: 40px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  /* 16px keeps iOS Safari from zooming in when a field gets focus. */
  font-size: 16px;
  background: var(--panel-bg);
  color: var(--text);
}

.field input:focus,
.edge-row input:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 3px rgba(242, 140, 6, 0.2);
}

.field input[type="color"] {
  width: 64px;
  height: 40px;
  padding: 3px;
  cursor: pointer;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  padding: 0 4px;
  margin: 0 -4px;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
}

.check-field input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin: 0;
  flex: none;
  accent-color: var(--accent-dark);
  cursor: pointer;
}

#ann-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.edge-row {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: 6px;
  align-items: center;
  font-size: 0.85rem;
}

/* Messages take no room until there is something to say. */
.error-message {
  color: var(--error);
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0;
}

.error-message:empty {
  display: none;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.button-row button {
  flex: 1 1 auto;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn {
  border: 1px solid var(--accent-dark);
  background: var(--accent-dark);
  color: #fff;
}

.button-row .primary-btn {
  flex-grow: 2;
}

.primary-btn:hover:not(:disabled) {
  filter: brightness(1.08);
}

.primary-btn:disabled {
  border-color: #c9c3ba;
  background: #c9c3ba;
  cursor: not-allowed;
}

.preview-surface {
  background: repeating-conic-gradient(#eceef1 0% 25%, #ffffff 0% 50%) 0 0 / 20px 20px;
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  height: max(320px, calc(100vh - 310px));
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-surface svg {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  background: transparent;
}

.preview-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 6px;
}

.jump-save {
  display: none;
}

/* ---------- export bar (under the preview) ---------- */

.export-bar {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.export-options {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 24px;
}

.export-options #png-custom-width-field input {
  width: 120px;
}

@media (max-width: 860px) {
  .app-main {
    grid-template-columns: minmax(0, 1fr);
    padding-top: 0;
  }
  .panel {
    position: static;
    max-height: none;
  }
  /* The preview, panel and export bar become rows of one column. */
  .stage {
    display: contents;
  }
  /* Keep the figure in view while scrolling through the settings. */
  .preview-area {
    order: -1;
    position: sticky;
    top: 0;
    z-index: 5;
    margin: 0 -16px;
    padding: 8px 16px 4px;
    background: var(--bg);
    box-shadow: 0 6px 8px -6px rgba(0, 0, 0, 0.18);
  }
  .preview-surface {
    height: 32vh;
    min-height: 180px;
    padding: 8px;
  }
  .preview-bar {
    justify-content: space-between;
    margin-top: 2px;
  }
  .jump-save {
    display: inline-flex;
    align-items: center;
    min-height: 32px;
    padding: 0 4px;
    border: none;
    background: none;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent-text);
    cursor: pointer;
  }
  .export-bar {
    order: 1;
    scroll-margin-top: calc(32vh + 60px);
  }
  .app-header {
    padding: 10px 16px;
  }
  .app-header h1 {
    font-size: 1rem;
  }
  .mode-btn {
    padding: 8px 16px;
  }
}

.hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-sub);
}


/* ---------- chip pickers (visual replacement for the hidden <select>s) ---------- */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel-bg);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

.chips-icons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
}

.chips-icons .chip {
  flex-direction: column;
  gap: 4px;
  min-height: 64px;
  border-radius: 8px;
  padding: 8px 4px 6px;
  font-size: 0.8rem;
}

.chip svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linejoin: round;
}

.chip:hover {
  border-color: var(--accent);
}

.chip:active,
.preset-btn:active,
.ghost-btn:active,
.stepper-btn:active,
.button-row button:active:not(:disabled) {
  transform: scale(0.97);
}

/* Remove the double-tap-zoom delay on touch controls. */
button,
input,
select,
.check-field {
  touch-action: manipulation;
}

.chip[aria-checked="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-text);
  font-weight: 600;
}

.chip:focus-visible,
.preset-btn:focus-visible,
.ghost-btn:focus-visible,
.secondary-btn:focus-visible,
.button-row button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset-btn {
  min-height: 36px;
  padding: 5px 12px;
  border: 1px dashed var(--accent);
  border-radius: 6px;
  background: transparent;
  color: var(--accent-text);
  font-size: 0.8rem;
  cursor: pointer;
}

.preset-btn:hover {
  background: var(--accent-soft);
}

/* ---------- header actions, secondary buttons, toast ---------- */

.app-header {
  gap: 12px;
  flex-wrap: wrap;
}

.header-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.ghost-btn {
  min-height: 40px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}

.ghost-btn:hover {
  border-color: var(--accent);
  color: var(--accent-text);
}

.secondary-btn {
  padding: 8px 12px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--panel-bg);
  color: var(--accent-text);
  font-size: 0.9rem;
  cursor: pointer;
}

.secondary-btn:hover:not(:disabled) {
  background: var(--accent-soft);
}

.secondary-btn:disabled {
  border-color: #c9c3ba;
  color: #c9c3ba;
  cursor: not-allowed;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 8px;
  background: #1a1d21;
  color: #fff;
  font-size: 0.9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  z-index: 10;
}

.toast.is-error {
  background: var(--error);
}

/* ---------- validation & preview feedback ---------- */

.field input[aria-invalid="true"],
.edge-row input[aria-invalid="true"] {
  border-color: var(--error);
  background: #fff5f5;
}

.preview-empty {
  margin: 0;
  max-width: 320px;
  padding: 16px 20px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  color: var(--text-sub);
  font-size: 0.9rem;
  text-align: center;
}

.preview-caption {
  margin: 6px 2px 0;
  font-size: 0.8rem;
  color: var(--text-sub);
  text-align: right;
}

@media (max-width: 860px) {
  .header-actions {
    order: 3;
    width: 100%;
    margin-left: 0;
  }
  .ghost-btn {
    flex: 1;
  }
}

.app-footer {
  padding: 24px 16px 32px;
  text-align: center;
  font-size: 12px;
  color: var(--text-sub);
}

.app-footer a {
  color: var(--text-sub);
  text-decoration: none;
}

.app-footer a:hover {
  color: var(--accent-text);
  text-decoration: underline;
}

.app-footer p {
  margin: 8px 0 0;
}

/* ---------- slider + number pairs ---------- */

.slider-pair {
  display: grid;
  grid-template-columns: 1fr 96px;
  gap: 12px;
  align-items: center;
}

/* A tall hit area and a large thumb so the slider is easy to grab by finger. */
.slider-pair input[type="range"] {
  order: -1;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 40px;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
}

.slider-pair input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: var(--border);
}

.slider-pair input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 3px;
  background: var(--border);
}

.slider-pair input[type="range"]::-moz-range-progress {
  height: 6px;
  border-radius: 3px;
  background: var(--accent);
}

.slider-pair input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 24px;
  height: 24px;
  margin-top: -9px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--accent-dark);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.slider-pair input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: var(--accent-dark);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

/* ---------- −/+ steppers ---------- */

.stepper {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: 6px;
}

.stepper input {
  grid-column: 2;
  grid-row: 1;
  text-align: center;
  min-width: 0;
}

.stepper .stepper-minus {
  grid-column: 1;
  grid-row: 1;
}

.stepper-btn {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.stepper-btn:hover {
  border-color: var(--accent);
  color: var(--accent-text);
}

/* ---------- line & fill colours ---------- */

.color-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 20px;
}

/* ---------- triangle helper ---------- */

.triangle-diagram svg {
  display: block;
  width: 100%;
  max-width: 220px;
  height: auto;
  margin: 0 auto;
}

.triangle-diagram .given {
  stroke: var(--accent);
  stroke-width: 3;
}

.triangle-diagram .other {
  stroke: #b5bcc6;
  stroke-width: 1.5;
  stroke-dasharray: 4 3;
}

.triangle-diagram text {
  font-size: 12px;
  fill: #555;
  stroke: none;
}

.triangle-diagram text.given-label {
  fill: var(--accent);
  font-weight: 600;
}

.measure-info {
  margin: 0;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--bg);
  font-size: 0.8rem;
  color: #333;
  line-height: 1.5;
}

.measure-info:empty {
  display: none;
}

.edge-row input[readonly] {
  background: var(--bg);
  color: #555;
}

/* Edge table: keep all three columns inside the fieldset. */
.edge-row {
  grid-template-columns: 2.6em minmax(0, 1fr) minmax(0, 1fr);
}

.edge-row > span {
  white-space: nowrap;
}

.edge-row input {
  width: 100%;
  min-width: 0;
  min-height: 38px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 16px;
}

#general-edges {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Space out the stacked fields inside each section. */
.mode-panel,
#shape3d-params,
#oblique-params,
.triangle-inputs {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
