:root {
  --bg: #14161b;
  --panel: #1c1f27;
  --panel-2: #232733;
  --line: #303646;
  --text: #e8e6df;
  --muted: #8b93a7;
  --accent: #e8a33d;
  --valley: #4d9fff;
  --mountain: #ff5d5d;
  --ok: #59c96a;
  --bad: #ff5d5d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body { display: flex; flex-direction: column; }

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.brand { font-size: 16px; }
.brand .tagline { color: var(--muted); font-size: 12px; margin-left: 10px; }

.header-controls { display: flex; align-items: center; gap: 8px; }
.header-controls label { color: var(--muted); }

main {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 340px 1fr 320px;
  min-height: 0;
}

section { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

#editor-panel { background: var(--panel); border-right: 1px solid var(--line); }
#chat-panel { background: var(--panel); border-left: 1px solid var(--line); }

.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 12px 6px;
  font-weight: 600;
}
.panel-title span:last-child { color: var(--muted); font-weight: 400; font-size: 12px; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  flex-wrap: wrap;
}
.toolbar .spacer { flex: 1; }
.toolbar label { color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }

button, select, input[type="text"] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  font: inherit;
  cursor: pointer;
}
button:hover, select:hover { border-color: var(--muted); }
button.primary { background: var(--accent); color: #1a1408; border-color: transparent; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }

.tool.active { outline: 2px solid var(--accent); }
.tool[data-tool="valley"] { color: var(--valley); }
.tool[data-tool="mountain"] { color: var(--mountain); }

#editor-svg {
  flex: 1 1 auto;
  min-height: 260px;
  margin: 4px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
}

#validity-report { color: var(--muted); font-size: 12px; }
#validity-report.ok { color: var(--ok); }
#validity-report.bad { color: var(--bad); }

/* ---------- 3D view ---------- */
#view-panel { position: relative; background: #0d0e12; }
#view3d { position: absolute; inset: 0; }
#view3d canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

#view-overlay {
  position: absolute;
  top: 10px; left: 14px;
  pointer-events: none;
}
#model-name { font-size: 18px; font-weight: 600; text-shadow: 0 1px 4px #000; }
#sim-status { color: var(--muted); font-size: 12px; }

#fold-controls {
  position: absolute;
  left: 50%; bottom: 14px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 10px;
  max-width: calc(100% - 16px);
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  backdrop-filter: blur(6px);
}
#fold-slider { width: clamp(110px, 24vw, 220px); }
#fold-label { min-width: 40px; text-align: right; color: var(--muted); }
#fold-controls label { color: var(--muted); display: inline-flex; gap: 5px; align-items: center; }

/* ---------- chat ---------- */
#chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  border-radius: 10px;
  padding: 8px 12px;
  max-width: 95%;
}
.msg.user { background: #2c3a55; align-self: flex-end; }
.msg.bot { background: var(--panel-2); align-self: flex-start; }
.msg p { margin: 4px 0; }
.msg ol { margin: 6px 0; padding-left: 20px; }
.msg li { margin: 5px 0; }
.msg li b { color: var(--accent); }
.msg .load-preset {
  margin-top: 6px;
  background: var(--accent);
  color: #1a1408;
  border: none;
  font-weight: 600;
}
.msg .meta { color: var(--muted); font-size: 12px; }
.msg.thinking { color: var(--muted); font-style: italic; }

#chat-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
}
#chat-input { flex: 1; cursor: text; }
#chat-mode { font-size: 11px; }

/* ---------- dialog ---------- */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  max-width: 520px;
  padding: 18px 22px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog a { color: var(--accent); }
dialog h2 { margin-top: 0; }

/* ---------- editor svg elements ---------- */
.grid-line { stroke: #333a4c; stroke-width: 0.002; }
.paper-face { fill: #f5f1e6; opacity: 0.06; }
.border-line { stroke: #aab; stroke-width: 0.006; fill: none; }
.crease-M { stroke: var(--mountain); stroke-width: 0.007; stroke-dasharray: 0.03 0.012 0.006 0.012; }
.crease-V { stroke: var(--valley); stroke-width: 0.007; stroke-dasharray: 0.025 0.015; }
.crease-F { stroke: #667; stroke-width: 0.004; }
.crease-preview { opacity: 0.55; }
.crease-hit { stroke: transparent; stroke-width: 0.035; cursor: pointer; }
.snap-dot { fill: var(--accent); }
.vertex-ok { fill: var(--ok); }
.vertex-bad { fill: var(--bad); stroke: #fff; stroke-width: 0.003; }

@media (max-width: 1100px) {
  body { height: auto; min-height: 100%; }
  main { grid-template-columns: 1fr; grid-template-rows: auto auto auto; }
  #editor-panel { order: 2; border-right: none; border-top: 1px solid var(--line); }
  #view-panel { order: 1; height: min(60vh, 480px); }
  #chat-panel { order: 3; min-height: 340px; border-left: none; border-top: 1px solid var(--line); }
  #chat-log { max-height: 50vh; }
  .brand .tagline { display: none; }
}
