:root {
  color-scheme: light;
  --background: #eef2f7;
  --background-accent: #f8fafc;
  --panel: #ffffff;
  --text: #172033;
  --muted: #5d697c;
  --border: #d9e0ea;
  --primary: #2f6fed;
  --primary-hover: #245dca;
  --primary-active: #1d4fac;
  --error: #b42318;
  --shadow: 0 18px 45px rgba(28, 39, 61, 0.12);
  --radius: 8px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(180deg, var(--background-accent) 0%, var(--background) 100%);
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

.page-shell {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.helper-card {
  width: min(100%, 640px);
  padding: 28px;
  background: var(--panel);
  border: 1px solid rgba(217, 224, 234, 0.8);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

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

.eyebrow {
  margin: 0 0 8px;
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-size: clamp(2rem, 8vw, 3.25rem);
  line-height: 1;
}

.subtitle {
  max-width: 34rem;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.6;
}

.helper-form {
  display: grid;
  gap: 18px;
}

.upload-section,
.clues-section {
  display: grid;
  gap: 14px;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafd;
}

.section-heading {
  display: grid;
  gap: 6px;
}

.section-heading h2,
.section-heading p {
  margin: 0;
}

.section-heading h2 {
  font-size: 1.05rem;
}

.section-heading p {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.field-group {
  display: grid;
  gap: 8px;
}

label {
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 700;
}

label span {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 500;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fbfcfe;
  color: var(--text);
  outline: none;
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease,
    background-color 160ms ease;
}

input {
  min-height: 46px;
  padding: 0 13px;
}

select {
  min-height: 46px;
  padding: 0 36px 0 13px;
}

textarea {
  min-height: 118px;
  padding: 12px 13px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  background: #ffffff;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.14);
}

.field-help,
.field-error {
  margin: 0;
  font-size: 0.86rem;
  line-height: 1.45;
}

.field-help {
  color: var(--muted);
}

.field-error {
  min-height: 1.25em;
  color: var(--error);
  font-weight: 600;
}

.primary-button {
  min-height: 48px;
  border: 0;
  border-radius: var(--radius);
  background: var(--primary);
  color: #ffffff;
  cursor: pointer;
  font-weight: 800;
  transition:
    background-color 160ms ease,
    transform 160ms ease,
    box-shadow 160ms ease;
}

.primary-button:hover,
.secondary-button:hover {
  background: var(--primary-hover);
  box-shadow: 0 10px 22px rgba(47, 111, 237, 0.25);
}

.primary-button:active,
.secondary-button:active {
  background: var(--primary-active);
  transform: translateY(1px);
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.7;
  transform: none;
}

.secondary-button {
  min-height: 44px;
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--primary);
  cursor: pointer;
  font-weight: 800;
  transition:
    background-color 160ms ease,
    color 160ms ease,
    transform 160ms ease,
    box-shadow 160ms ease;
}

.secondary-button:hover,
.secondary-button:active {
  color: #ffffff;
}

.preview-frame {
  width: 100%;
  max-height: 220px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
}

.preview-frame img {
  display: block;
  width: 100%;
  max-height: 220px;
  object-fit: contain;
}

.ocr-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.ghost-button {
  border-color: var(--border);
  color: var(--muted);
}

#cancel-parse-button {
  border-color: var(--error);
  color: var(--error);
}

#cancel-parse-button:hover,
#cancel-parse-button:active {
  background: var(--error);
  color: #ffffff;
  box-shadow: 0 10px 22px rgba(191, 42, 42, 0.2);
}

.ocr-status {
  min-height: 1.35em;
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.5;
}

.ocr-status.error {
  color: var(--error);
}

.ocr-status.success {
  color: #13795b;
}

.raw-ocr-panel,
.grid-debug-panel {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.raw-ocr-panel summary,
.grid-debug-panel summary {
  color: var(--primary);
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 800;
}

.raw-ocr-panel pre {
  max-height: 220px;
  margin: 12px 0 0;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
}

.debug-canvas-wrap {
  margin-top: 12px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
}

.debug-metrics {
  display: grid;
  grid-template-columns: minmax(130px, 180px) minmax(0, 1fr);
  gap: 6px 12px;
  margin: 12px 0 0;
  color: var(--text);
  font-size: 0.84rem;
}

.debug-metrics dt {
  color: var(--muted);
  font-weight: 800;
}

.debug-metrics dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
}

#grid-debug-canvas {
  display: block;
  width: 100%;
  max-height: 360px;
  object-fit: contain;
}

.debug-table-wrap {
  max-height: 220px;
  margin-top: 12px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
}

.debug-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.debug-table th,
.debug-table td {
  padding: 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.debug-table th {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.clue-toolbar {
  display: grid;
  grid-template-columns: minmax(120px, 180px) auto;
  gap: 10px;
  align-items: center;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.clue-list {
  display: grid;
  gap: 12px;
}

.clue-row {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) minmax(150px, auto) 42px;
  gap: 10px;
  align-items: start;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
}

.clue-length-field,
.clue-main-field,
.clue-pattern-field {
  display: grid;
  gap: 6px;
}

.clue-row label {
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.clue-length-input {
  min-height: 42px;
  padding: 0 8px;
  text-align: center;
}

.clue-type-select {
  min-height: 38px;
}

.clue-text-input {
  min-height: 42px;
}

.image-clue-upload {
  display: grid;
  gap: 8px;
}

.image-clue-preview {
  display: grid;
  place-items: center;
  min-height: 84px;
  overflow: hidden;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: #f8fafd;
  color: var(--muted);
  font-size: 0.9rem;
}

.image-clue-preview img {
  display: block;
  width: 100%;
  max-height: 120px;
  object-fit: contain;
}

.pattern-boxes {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 42px;
  align-items: center;
}

.pattern-box {
  width: 34px;
  height: 38px;
  padding: 0;
  border: 1px solid #b9c4d3;
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  font-weight: 800;
  text-align: center;
  text-transform: uppercase;
}

.pattern-box:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.18);
}

.pattern-placeholder {
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.4;
}

.remove-clue-button {
  width: 42px;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1;
}

.remove-clue-button:hover {
  border-color: var(--error);
  color: var(--error);
}

.empty-clues-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
}

.results-panel {
  margin-top: 24px;
  padding-top: 22px;
  border-top: 1px solid var(--border);
}

h2 {
  margin-bottom: 10px;
  font-size: 1.05rem;
}

.results-content {
  min-height: 88px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafd;
  color: var(--muted);
  line-height: 1.55;
}

.result-list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.result-item {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--text);
}

.result-item strong {
  display: block;
  margin-bottom: 4px;
}

.result-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

@media (max-width: 520px) {
  .page-shell {
    align-items: flex-start;
    padding: 16px;
  }

  .helper-card {
    padding: 22px 18px;
  }

  h1 {
    font-size: 2.3rem;
  }

  .clue-toolbar {
    grid-template-columns: 1fr;
  }

  .clue-row {
    grid-template-columns: 1fr;
  }

  .remove-clue-button {
    width: 100%;
  }
}

@media (min-width: 640px) {
  .ocr-actions {
    grid-template-columns: 1fr 1fr;
  }
}
