:root {
  color-scheme: light;
  --page: #f3f1ec;
  --panel: #fffdf9;
  --ink: #242321;
  --muted: #74716b;
  --line: #d8d4cc;
  --accent: #315c4c;
  --accent-hover: #24483b;
  --cell-line: rgba(24, 24, 24, 0.28);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 0;
  min-height: 100vh;
  background: var(--page);
  color: var(--ink);
}

button, input { font: inherit; }

.app-shell {
  position: relative;
  z-index: 1;
  width: min(1400px, calc(100% - 56px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.app-header {
  margin-bottom: 14px;
}

.header-copy {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto;
  gap: 8px;
  min-width: 0;
}
.site-title {
  width: min(100%, 585px);
  margin: -12px 0 0;
  line-height: 0;
}
.site-title img { display: block; width: 100%; height: auto; }
.header-text {
  width: 100%;
  max-width: none;
  padding: 0;
}
.character-decoration {
  position: fixed;
  z-index: 0;
  top: 52%;
  right: clamp(12px, 2vw, 38px);
  width: auto;
  max-width: 42vw;
  height: 82vh;
  max-height: 900px;
  object-fit: contain;
  transform: translateY(-50%);
  pointer-events: none;
  user-select: none;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 750;
  letter-spacing: 0.13em;
  text-transform: uppercase;
}

h1, h2, p { margin-top: 0; }
h2 { margin-bottom: 0; font-size: 1rem; }
.intro {
  max-width: none;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: nowrap;
}

.actions { display: flex; gap: 10px; flex: 0 0 auto; }
.button { min-height: 44px; padding: 0 20px; border: 1px solid transparent; border-radius: 8px; cursor: pointer; font-weight: 700; }
.button-secondary { border-color: #bcb7ae; background: var(--panel); color: var(--ink); }
.button-primary { background: var(--accent); color: white; }
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button-primary:hover:not(:disabled) { background: var(--accent-hover); }
.button:focus-visible { outline: 3px solid rgba(49, 92, 76, 0.25); outline-offset: 2px; }
.button:disabled { cursor: not-allowed; opacity: 0.42; }

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 0.35fr) minmax(540px, 0.65fr);
  height: clamp(600px, calc(100vh - 330px), 760px);
  gap: 22px;
  align-items: stretch;
}
.source-column { display: grid; grid-template-rows: minmax(0, 1.7fr) minmax(0, 1fr); gap: 14px; min-width: 0; min-height: 0; }
.panel { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); box-shadow: 0 8px 28px rgba(40, 36, 30, 0.045); }
.preview-panel, .stage-one-panel, .result-panel { min-width: 0; min-height: 0; padding: 14px; }
.preview-panel, .stage-one-panel { display: flex; flex-direction: column; }
.result-panel { position: relative; display: flex; flex-direction: column; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 10px; }
.muted { margin: 0; color: var(--muted); font-size: 0.85rem; }
.result-panel #patternStatus { position: absolute; top: 14px; right: 16px; }
.language-switcher {
  position: absolute;
  z-index: 2;
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #f2efe9;
}
.language-option {
  min-height: 26px;
  padding: 2px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 0.72rem;
  cursor: pointer;
}
.language-option.is-active { background: var(--panel); color: var(--ink); box-shadow: 0 1px 4px rgba(40, 36, 30, 0.12); }
.language-option:focus-visible { outline: 2px solid rgba(49, 92, 76, 0.35); outline-offset: 1px; }

.creator-footer {
  position: absolute;
  z-index: 2;
  display: flex;
  width: 220px;
  flex-direction: column;
  gap: 1px;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.35;
  opacity: 0.7;
}
.creator-footer a { color: inherit; text-decoration-thickness: 1px; text-underline-offset: 2px; }

.preview-frame {
  position: relative;
  display: grid;
  width: auto;
  max-width: 100%;
  min-height: 0;
  flex: 1 1 0;
  align-self: center;
  aspect-ratio: 1;
  place-items: center;
  overflow: hidden;
  border: 1px dashed #c9c4bb;
  border-radius: 8px;
  background: #eeece7;
  color: var(--muted);
}
.preview-frame img { width: 100%; height: 100%; object-fit: contain; }
.preview-canvas { display: block; width: 100%; height: 100%; background: #fff; }
.preview-canvas[hidden] { display: none; }
.preview-frame p { padding: 24px; margin: 0; text-align: center; }
.preview-frame[role="button"] { cursor: pointer; transition: border-color 150ms ease, background 150ms ease, box-shadow 150ms ease; }
.preview-frame[role="button"]:focus-visible { outline: 3px solid rgba(49, 92, 76, 0.25); outline-offset: 2px; }
.preview-frame.is-dragging { border-color: var(--accent); background: #e5eee9; box-shadow: inset 0 0 0 2px rgba(49, 92, 76, 0.12); }
.drop-prompt { display: flex; flex-direction: column; gap: 5px; }
.drop-prompt strong { color: var(--ink); font-size: 1rem; }
.drop-prompt span { font-size: 0.85rem; }
.drop-prompt small { margin-top: 4px; font-size: 0.72rem; }
.preview-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin-top: 8px; }
.preview-actions .button { width: 100%; }
.crop-selection {
  position: absolute;
  z-index: 3;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.75), 0 0 0 9999px rgba(10, 13, 18, 0.52);
  cursor: move;
  touch-action: none;
}
.crop-selection[hidden] { display: none; }
.crop-handle {
  position: absolute;
  width: 22px;
  height: 22px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
  touch-action: none;
}
.crop-handle-nw { top: 0; left: 0; transform: translate(-50%, -50%); cursor: nwse-resize; }
.crop-handle-ne { top: 0; right: 0; transform: translate(50%, -50%); cursor: nesw-resize; }
.crop-handle-sw { bottom: 0; left: 0; transform: translate(-50%, 50%); cursor: nesw-resize; }
.crop-handle-se { right: 0; bottom: 0; transform: translate(50%, 50%); cursor: nwse-resize; }
.crop-magnifier {
  position: fixed;
  z-index: 20;
  width: min(280px, 38vw);
  height: auto;
  border: 3px solid #fff;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 12px 38px rgba(0, 0, 0, 0.28);
  pointer-events: none;
  image-rendering: auto;
}
.crop-magnifier[hidden] { display: none; }

.stage-one-frame { width: auto; max-width: 100%; min-height: 0; flex: 1 1 0; aspect-ratio: 1; align-self: center; overflow: hidden; border: 1px solid var(--line); border-radius: 8px; background: #eeece7; }
.stage-one-frame canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; }

.grid-stage { display: grid; flex: 1 1 0; min-width: 0; min-height: 0; place-items: center; width: 100%; }
.pattern-image { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; object-fit: contain; image-rendering: pixelated; }
.pattern-image[hidden] { display: none; }
.debug-pattern-grid { display: none; }
.bead-grid {
  display: grid;
  grid-template-columns: repeat(24, 1fr);
  grid-template-rows: repeat(24, 1fr);
  width: min(100%, 720px);
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border-top: 1px solid var(--cell-line);
  border-left: 1px solid var(--cell-line);
  background: #e4e1da;
}
.bead-cell {
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--cell-line);
  border-bottom: 1px solid var(--cell-line);
  background: #f7f5f0;
  cursor: pointer;
}
.bead-cell:focus-visible { position: relative; z-index: 1; outline: 2px solid var(--accent); outline-offset: -2px; }
.result-actions { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 8px; }
.grid-toggle { display: inline-flex; align-items: center; gap: 7px; min-height: 44px; color: var(--ink); font-size: 0.86rem; cursor: pointer; }
.grid-toggle input { width: 17px; height: 17px; accent-color: var(--accent); }

.colors-panel { margin-top: 8px; padding: 14px; }
.colors-heading { align-items: flex-start; }
.colors-heading h2 { margin-bottom: 4px; }
.total-count { color: var(--accent); font-size: 0.9rem; white-space: nowrap; }
.colors-used { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.color-item { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 7px 10px; border: 1px solid #e1ddd5; border-radius: 7px; background: #faf8f4; }
.swatch { width: 30px; height: 30px; border: 1px solid rgba(0, 0, 0, 0.2); border-radius: 5px; }
.color-details { display: flex; min-width: 0; flex-direction: column; gap: 2px; }
.color-id { font-size: 0.78rem; font-weight: 750; }
.hex { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 0.82rem; font-weight: 700; }
.bead-count { color: var(--muted); font-size: 0.8rem; }
.tone-stats { grid-column: 2 / -1; color: var(--muted); font-size: 0.72rem; line-height: 1.35; }
.diagnostic-line { margin: 14px 0 0; color: var(--muted); font-size: 0.85rem; }
.trace-panel { margin-top: 20px; padding: 18px; }
.trace-summary { margin: 0 0 12px; font-size: 0.85rem; line-height: 1.55; }
.trace-list { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.trace-item { padding: 10px 12px; border: 1px solid #e1ddd5; border-radius: 7px; background: #faf8f4; font: 0.76rem/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }
.cell-debug-panel { margin-top: 20px; padding: 18px; }
.cell-debug-summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.debug-card { padding: 12px; border: 1px solid #e1ddd5; border-radius: 7px; background: #faf8f4; }
.debug-card h3 { margin: 0 0 8px; font-size: 0.85rem; }
.debug-card dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 0.8rem; }
.debug-card dt { color: var(--muted); }
.debug-card dd { margin: 0; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.candidate-table-wrap { overflow-x: auto; }
.candidate-table { width: 100%; border-collapse: collapse; font-size: 0.78rem; }
.candidate-table th, .candidate-table td { padding: 8px 9px; border-bottom: 1px solid #e1ddd5; text-align: right; white-space: nowrap; }
.candidate-table th:nth-child(2), .candidate-table th:nth-child(3), .candidate-table td:nth-child(2), .candidate-table td:nth-child(3) { text-align: left; }
.candidate-table th { color: var(--muted); font-weight: 700; }
.debug-subheading { margin: 4px 0 8px; font-size: 0.82rem; }

@media (max-width: 1600px) {
  .character-decoration {
    right: 1vw;
    max-width: 32vw;
    height: 68vh;
  }
}

@media (max-width: 1050px) {
  .language-switcher {
    position: static;
    width: max-content;
    margin: 0 0 10px auto;
  }
  .creator-footer {
    position: static;
    width: calc(100% - 36px);
    margin: 20px auto 24px;
    align-items: center;
    text-align: center;
  }
}

@media (max-width: 1050px) {
  .app-shell { width: calc(100% - 36px); }
  .site-title { width: min(100%, 585px); margin: -8px 0 0; }
  .header-text { width: 100%; max-width: none; padding: 0; }
  .workspace { grid-template-columns: 1fr; height: auto; min-height: 0; }
  .source-column, .result-panel { width: min(100%, 720px); justify-self: center; }
  .source-column { grid-template-rows: auto; }
  .preview-frame { width: 100%; flex: none; }
  .stage-one-frame { width: min(100%, 330px); flex: none; }
  .pattern-image { width: min(100%, 800px); height: auto; max-height: none; }
  .character-decoration { display: none; }
  .result-panel #patternStatus { position: static; }
}

@media (max-width: 620px) {
  .app-shell { width: calc(100% - 24px); padding-top: 24px; }
  .app-header { gap: 14px; }
  .site-title img { width: 100%; }
  .header-text { max-width: 100%; }
  .intro { font-size: 0.86rem; white-space: normal; }
  .result-panel > .panel-heading { align-items: flex-start; }
  .language-option { padding-inline: 6px; }
  .preview-panel, .stage-one-panel, .result-panel, .colors-panel { padding: 14px; }
  .workspace .result-panel {
    height: auto;
    min-height: 0;
    overflow: visible;
  }
  .result-panel .grid-stage {
    position: relative;
    display: block;
    flex: none;
    width: 100%;
    height: auto;
    min-height: auto;
    overflow: visible;
  }
  .result-actions {
    position: static !important;
    inset: auto !important;
    transform: none !important;
    flex: 0 0 auto;
    align-items: center;
    flex-direction: column;
    width: 100%;
    gap: 10px;
    margin-top: 12px;
  }
  .result-actions .button { width: auto; }
  .grid-toggle { justify-content: center; }
  .cell-debug-summary { grid-template-columns: 1fr; }
}
