/* ===========================================================
   Tessellation Studio — tool styles
   light paper / ink / serif aesthetic, matching ES Transform
   =========================================================== */

:root {
  --ink: #1a1410;
  --paper: #ede4d3;
  --paper-dark: #d9cdb4;
  --accent: #8b3a1f;
  --line: #3a2a1f;
  --shadow: rgba(26, 20, 16, 0.15);
  --radius: 0px;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

.lucide, svg.lucide {
  width: 1.05em; height: 1.05em;
  stroke-width: 2; vertical-align: -0.15em;
}
button .lucide, #aboutLink .lucide { vertical-align: -0.18em; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: 'Cormorant Garamond', 'Shippori Mincho', serif;
  font-synthesis: none;
  font-size: 15px;
}

body { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; animation: toolFadeIn 0.32s ease both; }
@keyframes toolFadeIn { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { body { animation: none; } }

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 24px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 {
  font-family: 'Cormorant Garamond', 'Shippori Mincho', serif;
  font-size: 22px; margin: 0; font-weight: 500; letter-spacing: -0.01em;
}
.brand h1 em { font-style: italic; color: var(--accent); }
.brand p {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px; margin: 0; color: var(--line); opacity: 0.7;
  letter-spacing: 0.12em; text-transform: uppercase;
}

/* mode switch — grouped bordered buttons (ES Transform fit-toggle style) */
.modes {
  display: flex; gap: 0;
  border: 1px solid var(--line);
  background: var(--paper);
}
.modes button {
  border: none; border-right: 1px solid var(--line);
  background: transparent; color: var(--ink);
  padding: 8px 14px; cursor: pointer; min-height: 38px;
  font-family: 'Cormorant Garamond', 'Shippori Mincho', serif;
  font-style: italic; font-size: 15px; font-weight: 500;
  transition: .2s;
}
.modes button:last-child { border-right: none; }
.modes button:hover { color: var(--accent); }
.modes button.active { background: var(--ink); color: var(--paper); }

.actions { margin-left: auto; display: flex; gap: 8px; align-items: stretch; }

/* mobile-only editor/preview tab switcher (hidden on desktop) */
.view-tabs { display: none; }

button {
  font-family: 'Cormorant Garamond', 'Shippori Mincho', serif;
  font-style: italic;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  padding: 8px 14px;
  cursor: pointer;
  font-size: 15px;
  min-height: 38px;
  transition: .2s;
}
button:hover { background: var(--ink); color: var(--paper); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button:disabled:hover { background: var(--paper); color: var(--ink); }
.btn-primary { background: var(--ink); color: var(--paper); border-color: var(--ink); font-weight: 500; }
.btn-primary:hover { background: var(--accent); border-color: var(--accent); }

#aboutLink {
  display: inline-flex; align-items: center; gap: 6px;
  text-decoration: none; color: var(--ink);
  border: 1px solid var(--line); background: var(--paper);
  padding: 8px 14px; font-style: italic; font-size: 15px; transition: .2s;
}
#aboutLink:hover { background: var(--ink); color: var(--paper); }

/* ---- Layout ---- */
.layout { flex: 1; display: grid; grid-template-columns: 360px 1fr; gap: 0; min-height: 0; }

.panel { padding: 22px; overflow-y: auto; background: var(--paper); }
.editor-panel { border-right: 1px solid var(--line); }
.preview-panel { display: flex; flex-direction: column; min-height: 0; }

.panel h2 {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.2em;
  color: var(--accent); margin: 26px 0 12px; opacity: 0.9;
}
.panel h2:first-child { margin-top: 0; }
.hint { font-size: 13px; color: var(--line); margin: 4px 0; font-style: italic; opacity: 0.85; }

.canvas-wrap {
  background: var(--paper-dark);
  border: 1px solid var(--line);
  box-shadow: 8px 8px 0 var(--ink);
  overflow: hidden; position: relative;
}
.editor-wrap { aspect-ratio: 1; width: calc(100% - 8px); margin-bottom: 4px; }
.editor-wrap canvas { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }

.preview-wrap { flex: 1; min-height: 0; margin: 0 8px 8px 0; }
.preview-wrap canvas { width: 100%; height: 100%; display: block; cursor: move; touch-action: none; }

/* ---- Edge controls ---- */
.edge-controls { display: flex; flex-direction: column; gap: 6px; margin-top: 14px; }
.edge-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--paper-dark); padding: 8px 10px; border: 1px solid var(--line);
}
.edge-row .label { flex: 1; font-size: 14px; font-style: italic; }
.edge-row button { padding: 2px 11px; font-size: 16px; line-height: 1; min-height: 28px; }

/* ---- Range control rows (smooth / outline / morph span / rotation) ---- */
.control-row { margin: 10px 0 4px; }
.control-label {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 15px; margin-bottom: 6px;
}
.control-label .value {
  font-family: 'JetBrains Mono', monospace; font-size: 12px;
  color: var(--accent); opacity: 0.9;
}

input[type="range"] {
  width: 100%;
  -webkit-appearance: none; appearance: none;
  background: transparent; height: 28px; cursor: pointer; touch-action: none;
}
input[type="range"]::-webkit-slider-runnable-track { height: 1px; background: var(--line); opacity: 0.6; }
input[type="range"]::-moz-range-track { height: 1px; background: var(--line); opacity: 0.6; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 20px; height: 20px; background: var(--accent); border: 2px solid var(--paper);
  margin-top: -10px; cursor: pointer; transform: rotate(45deg);
}
input[type="range"]::-moz-range-thumb {
  width: 20px; height: 20px; background: var(--accent); border: 2px solid var(--paper);
  border-radius: 0; cursor: pointer;
}

/* ---- Palette ---- */
.palette { display: flex; flex-wrap: wrap; gap: 8px; }
.palette .color-chip { position: relative; }
.palette input[type=color] {
  width: 44px; height: 36px; border: 1px solid var(--line); background: none; cursor: pointer; padding: 2px;
}
.palette .remove {
  position: absolute; top: -6px; right: -6px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); color: var(--paper); border: none; font-size: 11px; line-height: 1;
  padding: 0; cursor: pointer; display: none; min-height: 0;
}
.palette .color-chip:hover .remove { display: block; }
.palette-actions { display: flex; gap: 8px; margin-top: 10px; }
.palette-actions button { flex: 1; font-size: 13px; }

/* ---- Morph mode ---- */
.editor-panel .morph-controls h2 { margin-top: 26px; }

.morph-switch {
  display: flex; gap: 0; border: 1px solid var(--line); background: var(--paper); margin-bottom: 12px;
}
.morph-switch button {
  flex: 1; border: none; border-right: 1px solid var(--line);
  background: transparent; color: var(--ink); font-weight: 500; padding: 8px;
}
.morph-switch button:last-child { border-right: none; }
.morph-switch button.active { background: var(--ink); color: var(--paper); }
.morph-switch button[data-shape="A"].active { box-shadow: inset 3px 0 0 var(--accent); }
.morph-switch button[data-shape="B"].active { box-shadow: inset -3px 0 0 var(--accent); }

.bg-row { display: flex; align-items: center; gap: 12px; }
.bg-row input[type=color] {
  width: 44px; height: 36px; border: 1px solid var(--line); background: none; cursor: pointer; padding: 2px;
}

/* ---- Tool credit / links ---- */
.tool-credit {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.tool-credit a {
  color: var(--line);
  text-decoration: none;
  opacity: 0.75;
  transition: .2s;
}
.tool-credit a:hover { color: var(--accent); opacity: 1; }

/* ---- Zoom controls ---- */
.zoom-controls { position: absolute; bottom: 12px; right: 12px; display: flex; gap: 6px; }
.zoom-controls button {
  width: 36px; height: 36px; padding: 0; font-size: 17px; font-style: normal;
  background: var(--paper); border: 1px solid var(--line);
  /* stop repeated taps from triggering the browser's double-tap-to-zoom */
  touch-action: manipulation;
}
/* invert fully on hover so the icon (currentColor) stays visible; otherwise the
   later .zoom-controls background wins over button:hover and the icon vanishes */
.zoom-controls button:hover { background: var(--ink); color: var(--paper); border-color: var(--ink); }

/* ---- Save modal (mobile long-press to save) ---- */
.save-modal {
  position: fixed; inset: 0;
  background: rgba(26, 20, 16, 0.97);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  z-index: 200; display: none; align-items: center; justify-content: center;
  padding: 1.25rem; overflow-y: auto;
}
.save-modal.show { display: flex; }
.save-modal-inner {
  display: flex; flex-direction: column; align-items: center; gap: 1rem; color: var(--paper);
  max-width: 100%; max-height: 100%;
}
.save-modal-img {
  max-width: min(90vw, 600px); max-height: 65vh;
  border: 1px solid var(--paper); box-shadow: 0 0 0 1px var(--ink), 4px 4px 0 var(--accent);
  -webkit-touch-callout: default; pointer-events: auto;
  user-select: none; -webkit-user-select: none;
}
.save-modal-instruction {
  font-family: 'Cormorant Garamond', 'Shippori Mincho', serif; font-style: italic;
  font-size: 0.95rem; text-align: center; max-width: 90vw; line-height: 1.5; opacity: 0.9;
}
.save-modal-instruction small {
  display: block; margin-top: 0.35rem;
  font-family: 'JetBrains Mono', monospace; font-style: normal; font-size: 0.62rem;
  opacity: 0.55; letter-spacing: 0.15em; text-transform: uppercase;
}
.save-modal-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; justify-content: center; }
.save-modal-actions button {
  background: transparent; color: var(--paper); border: 1px solid var(--paper);
  padding: 0.7rem 1.25rem; font-style: italic; font-size: 1rem; min-height: 44px;
}
.save-modal-actions button.primary { background: var(--paper); color: var(--ink); }
.save-modal-actions button:hover { background: var(--accent); color: var(--paper); border-color: var(--accent); }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .editor-panel { border-right: none; border-bottom: 1px solid var(--line); max-height: 52vh; }
  .topbar { gap: 12px; padding: 12px 16px; }
  .modes button { padding: 7px 10px; font-size: 13px; }
}

/* tablet range: brand takes its own row so modes (left) + actions (right) form a clean second row */
@media (max-width: 1100px) and (min-width: 621px) {
  .brand { flex-basis: 100%; }
}

/* phones: stack the topbar so the mode/action rows never overflow horizontally */
@media (max-width: 620px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .modes { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .modes button { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); min-height: 44px; }
  .modes button:nth-child(2n) { border-right: none; }
  .modes button:nth-child(n + 3) { border-bottom: none; }
  .actions { margin-left: 0; width: 100%; flex-wrap: wrap; gap: 6px; }
  .actions > * { flex: 1 1 auto; justify-content: center; min-height: 44px; }

  /* show only one full-height panel at a time, switched by the tab bar */
  .view-tabs { display: flex; border-bottom: 1px solid var(--line); background: var(--paper); }
  .view-tabs button {
    flex: 1 1 0; min-height: 44px; border: none; border-right: 1px solid var(--line);
    font-size: 14px; color: var(--line); opacity: 0.7;
  }
  .view-tabs button:last-child { border-right: none; }
  .view-tabs button.active { color: var(--paper); background: var(--ink); opacity: 1; }

  /* these hints describe mouse-only gestures (double-click / wheel / drag) — hide on touch */
  .editor-panel > p.hint, .preview-panel h2 .hint { display: none; }

  .layout { display: block; grid-template-columns: none; grid-template-rows: none; }
  .editor-panel { max-height: none; }
  .editor-panel, .preview-panel { display: none; height: 100%; border-bottom: none; }
  .layout.show-editor .editor-panel { display: block; }
  .layout.show-preview .preview-panel { display: flex; }
}
