:root {
  --ink: #1a1a1a;
  --paper: #ffffff;
  --line: #dcdcdc;
  --accent: #1a1a1a;
  --accent-text: #ffffff;
  --muted: #666;
  --error-bg: #fdecea;
  --error-text: #611a15;
  --error-border: #f5c6cb;
  --success-bg: #eafaf0;
  --success-text: #1c5231;
  --success-border: #b7e4c7;
  /* Tells the browser this page supports both schemes, so native controls (text
     inputs, selects, checkboxes, scrollbars) auto-adapt without needing to hand-style
     every one of them individually. */
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e8e8e8;
    --paper: #121212;
    --line: #3a3a3a;
    --accent: #e8e8e8;
    --accent-text: #121212;
    --muted: #999;
    --error-bg: #3a1f1f;
    --error-text: #ffb4a8;
    --error-border: #5a2a2a;
    --success-bg: #1c3324;
    --success-text: #a8e6bf;
    --success-border: #2d5940;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--paper);
}
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; text-decoration: none; color: var(--ink); }
nav { display: flex; gap: 1.25rem; flex-wrap: wrap; }
nav a { color: var(--muted); text-decoration: none; font-size: 0.95rem; }
nav a:hover { color: var(--ink); }
main { max-width: 900px; margin: 0 auto; padding: 2rem 1.5rem 4rem; }
h1 { margin-bottom: 0.25rem; }
.lede { color: var(--muted); max-width: 65ch; }
.hint { color: var(--muted); font-size: 0.85rem; margin-top: -0.5rem; }

.tool-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-top: 1.5rem;
}
@media (max-width: 700px) {
  .tool-grid { grid-template-columns: 1fr; }
}
.tool-card {
  display: block;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1.25rem;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s;
}
.tool-card:hover { border-color: var(--accent); }
.tool-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  margin-bottom: 0.75rem;
  border-radius: 4px;
  background: #fafafa;
}
.tool-card h2 { margin: 0 0 0.5rem; font-size: 1.1rem; }
.tool-card p { margin: 0; color: var(--muted); font-size: 0.9rem; }

/* Homepage "Utilities": helper tools (e.g. Infill Circles, which works on an uploaded
   SVG rather than generating one), listed as plain links below the generator cards. */
.utilities { margin-top: 2.5rem; }
.utilities h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
.utility-list { list-style: none; margin: 0; padding: 0; }
.utility-list li { padding: 0.6rem 0; border-top: 1px solid var(--line); }
.utility-list li:last-child { border-bottom: 1px solid var(--line); }
.utility-list a { color: var(--ink); font-weight: 600; }
.utility-list p { margin: 0.25rem 0 0; color: var(--muted); font-size: 0.9rem; }

.input-details { max-width: 640px; }
.input-details summary {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin: 1.5rem 0 0;
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  user-select: none;
  list-style: none;
}
.input-details summary::-webkit-details-marker { display: none; }
.input-details summary::before {
  content: "▶";
  display: inline-block;
  font-size: 0.75em;
  transition: transform 0.15s ease;
}
.input-details[open] summary::before { transform: rotate(90deg); }
.input-details summary:hover { border-color: var(--accent); }
.input-details[open] summary { margin-bottom: 0.5rem; }

.tool-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  margin: 1.5rem 0;
  max-width: 640px;
}
.tool-form label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.9rem;
}
.tool-form input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0.4rem 0.5rem;
  font-size: 0.9rem;
  width: 8ch;
  flex: none;
}
.tool-form input[type="text"].wide-input { width: 16ch; }
.tool-form input[type="text"].k-input { width: 12ch; }
.tool-form input[type="text"]:read-only { background: var(--line); opacity: 0.75; }
.tool-form input[type="file"],
.tool-form select {
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0.4rem 0.6rem;
  font-size: 0.9rem;
  flex: 1;
  max-width: 220px;
}
.tool-form input[type="checkbox"] { margin-right: 0.5rem; }
.dropzone {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 2px dashed var(--line);
  border-radius: 8px;
  padding: 1.5rem 1rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover { border-color: var(--accent); }
.dropzone.dragover { border-color: var(--accent); border-style: solid; }
.dropzone input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  opacity: 0;
}
.dropzone .dropzone-hint { color: var(--muted); font-size: 0.85rem; }
.dropzone .dropzone-filename { font-size: 0.9rem; font-weight: 600; }
.tool-form fieldset {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.75rem 1rem 1rem;
}
.tool-form fieldset label { margin-top: 0.6rem; }
.tool-form legend { font-size: 0.8rem; color: var(--muted); padding: 0 0.4rem; }
.tool-form .radio-group { display: flex; gap: 0.5rem 1rem; flex: 1; flex-wrap: wrap; justify-content: flex-end; }

.tool-form .input-row {
  display: flex;
  gap: 1.25rem;
  flex-wrap: wrap;
}
.tool-form .slider-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.tool-form .slider-field input[type="range"] { width: 100%; }
.tool-form .slider-endpoints {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted);
}
.tool-form .input-row label {
  justify-content: flex-start;
  gap: 0.4rem;
}
.tool-form label.inline {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.9rem;
  white-space: nowrap;
}
.tool-form label.inline input[type="radio"] { margin: 0; }
.tool-form button {
  align-self: flex-start;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 6px;
  padding: 0.55rem 1.4rem;
  font-size: 0.95rem;
  cursor: pointer;
}
.tool-form button:hover { opacity: 0.85; }
.tool-form button.secondary {
  align-self: auto;
  flex: none;
  background: none;
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 0.4rem 0.9rem;
  font-size: 0.9rem;
}
.tool-form button.secondary:hover { opacity: 1; border-color: var(--accent); }
.tool-form button.secondary:disabled { opacity: 0.4; cursor: not-allowed; border-color: var(--line); }
.tool-form .draw-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0.6rem 0 0.9rem;
  font-size: 0.9rem;
}
.tool-form .draw-row span { overflow-wrap: anywhere; }

/* Greek key's "Draw the pattern" modal (see key.html). */
.draw-dialog {
  width: min(440px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  padding: 1.25rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: var(--paper);
}
.draw-dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
.draw-dialog h2 { margin: 0 0 1rem; font-size: 1.1rem; }
.draw-dialog label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.9rem;
}
.draw-dialog input[type="text"] {
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0.4rem 0.5rem;
  font-size: 0.9rem;
  flex: 1;
  min-width: 0;
  max-width: 24ch;
}
.draw-dialog .hint { margin-top: 0.5rem; }
.draw-dialog label[hidden] { display: none; }
.draw-dialog .draw-grid-size { justify-content: flex-start; margin-top: 0.75rem; }
.draw-dialog select {
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0.35rem 0.5rem;
  font-size: 0.9rem;
}
.draw-dialog.draw-dialog-wide { width: min(680px, calc(100vw - 2rem)); }
.draw-dialog.draw-dialog-wide .draw-grid { max-width: 600px; }
.draw-grid {
  display: block;
  width: 100%;
  max-width: 360px;
  margin: 0 auto;
  touch-action: manipulation;
  user-select: none;
}
.draw-grid .grid-line { stroke: var(--line); stroke-width: 1; }
.draw-grid .draw-path {
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.5;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.draw-grid .draw-shape { fill: none; stroke: var(--ink); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.draw-grid .draw-preview { fill: none; stroke: var(--muted); stroke-width: 2; stroke-dasharray: 4 4; }
.draw-tools { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; margin: 1rem 0 0.5rem; }
.draw-tools .draw-spacer { flex: 1; }
.draw-tools[hidden] { display: none; }
#draw-segments { margin-top: 0; }
.draw-dialog .draw-tools button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}
.draw-grid .dot { cursor: pointer; }
.draw-grid .dot .hit { fill: transparent; }
.draw-grid .dot .mark { fill: var(--muted); }
.draw-grid .dot.on-path .mark,
.draw-grid .dot:not(.unreachable):hover .mark { fill: var(--ink); }
.draw-grid .dot.unreachable { cursor: not-allowed; }
.draw-grid.erasing .dot:not(.unreachable):hover .mark { fill: var(--error-text); }
.draw-grid .dot.unreachable:not(.on-path) .mark { opacity: 0.3; }
.draw-status {
  min-height: 2.6em;
  margin: 0.5rem 0;
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
  white-space: pre-line;
}
.draw-error { margin: 0.5rem 0; color: var(--error-text); font-size: 0.85rem; }
.draw-actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.draw-actions .draw-spacer { flex: 1; }
.draw-dialog button {
  background: none;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
  cursor: pointer;
}
.draw-dialog button:hover { border-color: var(--accent); }
.draw-dialog button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}
.draw-dialog button:disabled { opacity: 0.4; cursor: not-allowed; }

.history-container {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin: 0.25rem 0 0.5rem;
}
.history-group {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.45rem;
}
.history-label { font-size: 0.8rem; color: var(--muted); }
.history-btn {
  background: none;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 999px;
  min-width: 1.8rem;
  height: 1.8rem;
  padding: 0 0.5rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.history-btn:hover { border-color: var(--accent); color: var(--accent); }

.error {
  background: var(--error-bg);
  color: var(--error-text);
  border: 1px solid var(--error-border);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin: 1rem 0;
}
.success {
  background: var(--success-bg);
  color: var(--success-text);
  border: 1px solid var(--success-border);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin: 1rem 0;
}
.result { margin-top: 1.5rem; }
.svg-frame {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  max-width: 100%;
  overflow: auto;
  /* Always white, not var(--paper) -- generated diagrams default to black strokes/fills
     (e.g. outer_fill="#000000"), which would go invisible against a dark page
     background. Same reasoning as .tool-thumb's fixed #fafafa on the index page. */
  background: #ffffff;
}
.svg-frame svg { max-width: 100%; height: auto; display: block; margin: 0 auto; }
.download-btn {
  display: inline-block;
  margin-top: 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
  border: none;
  border-radius: 6px;
  padding: 0.55rem 1.4rem;
  font-size: 0.95rem;
}
.download-btn:hover { opacity: 0.85; }
.stats { color: var(--muted); font-size: 0.85rem; margin-top: 0.75rem; }

footer {
  border-top: 1px solid var(--line);
  padding: 1.25rem 1.5rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}
.copyleft {
  display: inline-block;
  transform: scaleX(-1);
}
.footer-note {
  margin: 0.35rem 0 0;
  font-size: 0.7rem;
  color: var(--muted);
}
