* {
  box-sizing: border-box;
}

:root {
  --bg: #ffffff;
  --bg-accent: #ffffff;
  --card: #ffffff;
  --ink: #101828;
  --muted: #5f6d7e;
  --line: #d4dcec;
  --primary: #4f883d;
  --primary-soft: rgba(79, 136, 61, 0.18);
  --radius-xl: 5px;
  --radius-lg: 5px;
  --shadow: 0 18px 40px rgba(15, 23, 42, 0.09);
  --given: #f6fbf4;
  --empty: #fcfdff;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  color: var(--ink);
  font-family: 'Nunito', sans-serif;
  background:
    radial-gradient(circle at 0 0, rgba(31, 122, 239, 0.1) 0%, transparent 42%),
    radial-gradient(circle at 100% 100%, rgba(15, 185, 130, 0.12) 0%, transparent 48%),
    var(--bg);
}

h1,
h2,
h3,
.brand-title {
  font-family: 'Space Grotesk', sans-serif;
}

.app-shell {
  width: 100%;
  margin: 0;
  padding: 10px;
  display: grid;
  grid-template-columns: 370px 1fr;
  gap: 18px;
  align-items: start;
}

.settings-panel,
.card {
  background: var(--card);
  border: 1px solid rgba(212, 220, 236, 0.8);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
}

.settings-panel {
  position: sticky;
  top: 16px;
  align-self: start;
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 18px;
}

.settings-panel::-webkit-scrollbar {
  width: 8px;
}

.settings-panel::-webkit-scrollbar-thumb {
  background: #c5d0e4;
  border-radius: 999px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 8px 14px;
}

.brand-icon {
  width: 38px;
  height: 38px;
  border-radius: 5px;
  display: grid;
  place-items: center;
  font-size: 23px;
  color: #fff;
  background: linear-gradient(135deg, #1f7aef, #14b8a6);
}

.brand-title {
  font-size: 1.12rem;
  font-weight: 700;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.92rem;
}

.panel-section {
  padding: 14px;
  border-radius: var(--radius-lg);
  border: 1px solid #e7edf8;
  background: linear-gradient(180deg, #fcfdff 0%, #f9fbff 100%);
  margin-bottom: 12px;
}

.panel-section h2 {
  margin: 0 0 12px;
  font-size: 1rem;
  font-weight: 700;
}

.field {
  display: block;
}

.field-label {
  display: block;
  margin: 10px 0 6px;
  color: #344054;
  font-size: 0.92rem;
  font-weight: 700;
}

.field-help {
  margin: -2px 0 10px;
  color: var(--muted);
  font-size: 0.85rem;
}

.field-input,
input,
select {
  width: 100%;
  border: 1px solid #c9d5ea;
  background: #fff;
  border-radius: 5px;
  padding: 10px 12px;
  font: inherit;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.field-input:focus,
input:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.field-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  align-items: end;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid #cfe0f6;
  border-radius: 5px;
  background: #f5f8ff;
  padding: 4px;
  gap: 4px;
}

.tab {
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 9px 10px;
  font: inherit;
  font-weight: 800;
  font-size: 0.92rem;
  color: #3f4b60;
  background: transparent;
  cursor: pointer;
  transition: transform 0.16s ease, background-color 0.16s ease, color 0.16s ease;
}

.tab:hover {
  transform: translateY(-1px);
}

.tab.is-active {
  background: #4f883d;
  color: #fff;
}

.ai-open-btn {
  width: auto;
  margin-top: 0;
  margin-left: auto;
  padding: 6px 9px;
  font-size: 0.8rem;
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  box-shadow: none;
  white-space: nowrap;
}

.ai-open-btn:hover {
  background: #406f31;
  border-color: #406f31;
}

.symbol-grid {
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.symbol-grid.upload-grid {
  grid-template-columns: 1fr;
}

.symbol-input {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px;
  align-items: center;
  background: #fff;
  border: 1px solid #d6e0ef;
  border-radius: 5px;
  padding: 8px;
}

.symbol-input label {
  font-size: 0.82rem;
  font-weight: 800;
  color: #3a4a45;
}

.symbol-input input {
  min-width: 0;
  border-radius: 5px;
  padding: 7px 8px;
  text-align: center;
  font-weight: 700;
}

.upload-row {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  background: #fff;
  border: 1px solid #d6e0ef;
  border-radius: 5px;
  padding: 8px;
}

.upload-row label {
  font-size: 0.82rem;
  font-weight: 800;
  color: #3a4a45;
}

.upload-row input[type='file'] {
  padding: 6px;
  font-size: 0.84rem;
}

.upload-thumb {
  width: 32px;
  height: 32px;
  border-radius: 5px;
  object-fit: cover;
  border: 1px solid #d9e3f0;
  background: #fff;
}

.gallery-pick-btn {
  padding: 8px 11px;
  font-size: 0.84rem;
  line-height: 1;
}

.gallery-pick-btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

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

.btn {
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 10px 12px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease, color 0.2s ease;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 8px 20px rgba(79, 136, 61, 0.28);
}

.btn-primary:hover {
  background: #406f31;
}

.btn-ghost {
  background: #fff;
  border-color: #b7d4ad;
  color: #3f6f30;
}

.btn-ghost:hover {
  background: #f2f8ef;
}

.workspace {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  padding: 16px;
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.card-top h2 {
  margin: 0;
  font-size: 1.45rem;
}

.card-top p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
}

.chip {
  border-radius: 5px;
  background: #f4f8ff;
  border: 1px solid #d2e0fa;
  color: #19458a;
  padding: 7px 11px;
  font-size: 0.83rem;
  font-weight: 700;
  white-space: nowrap;
}

.preview-canvas {
  margin-top: 14px;
  overflow: auto;
  border: 1px solid #dae4f4;
  border-radius: 5px;
  background: #fbfdff;
  padding: 12px;
}

.cutout-section {
  margin-top: 12px;
  border: 1px dashed #bcd0eb;
  border-radius: 5px;
  background: #f8fbff;
  padding: 10px;
}

.cutout-section h3 {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

.cutout-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 6px;
}

.cutout-grid.text-fill-grid {
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}

.text-fill-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid #c7d6ec;
  border-radius: 5px;
  background: #fff;
  padding: 8px 10px;
}

.text-fill-label {
  font-weight: 800;
  color: #1f2937;
  overflow-wrap: anywhere;
}

.text-fill-count {
  flex-shrink: 0;
  border-radius: 999px;
  background: #e7f0ff;
  border: 1px solid #c4d6fb;
  color: #1e3a8a;
  padding: 2px 8px;
  font-weight: 800;
  font-size: 0.84rem;
}

.cutout-item {
  aspect-ratio: 1;
  border: 1px dashed #9db7de;
  background: #fff;
  border-radius: 4px;
  padding: 4px;
  display: grid;
  place-items: center;
}

.cutout-item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.cutout-fallback {
  font-weight: 800;
  color: #334155;
}

.sudoku-board {
  width: min(100%, 760px);
  aspect-ratio: 1;
  margin: 0 auto;
  border: 1px solid #cfdaee;
  background: #fff;
  display: grid;
  grid-template-columns: repeat(var(--grid-size, 9), 1fr);
  grid-template-rows: repeat(var(--grid-size, 9), 1fr);
}

.cell {
  border: 1px solid rgba(146, 170, 201, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  background: var(--empty);
  color: #1f2e3c;
  user-select: none;
  line-height: 1;
  font-size: clamp(0.72rem, 1.9vw, 1.2rem);
  overflow: hidden;
  padding: 2px;
}

.cell.given {
  background: var(--given);
  color: #153e2a;
}

.cell.empty {
  color: #b6c2d1;
}

.cell.box-right {
  border-right: 2px solid #6f89af;
}

.cell.box-bottom {
  border-bottom: 2px solid #6f89af;
}

.cell-image {
  width: 82%;
  height: 82%;
  object-fit: contain;
}

.cell-text {
  display: block;
  width: 100%;
  text-align: center;
  white-space: pre-line;
  line-height: 1.04;
}

[hidden] {
  display: none !important;
}

.gallery-modal {
  position: fixed;
  inset: 0;
  z-index: 1200;
}

.gallery-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
}

.gallery-modal-dialog {
  position: relative;
  width: min(1040px, calc(100vw - 40px));
  max-height: calc(100vh - 40px);
  margin: 20px auto;
  background: #fff;
  border: 1px solid #d4dcec;
  border-radius: 5px;
  box-shadow: 0 25px 60px rgba(15, 23, 42, 0.28);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.gallery-modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid #e2e8f6;
  background: #f8fbff;
}

.gallery-modal-head h3 {
  margin: 0;
  font-size: 1.02rem;
}

.gallery-close-btn {
  width: 34px;
  height: 34px;
  border: 1px solid #ccdaef;
  border-radius: 5px;
  background: #fff;
  color: #334155;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
}

.gallery-tabs {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 7px;
  padding: 10px 14px 8px;
  border-bottom: 1px solid #e2e8f6;
  overflow-x: hidden;
  overflow-y: auto;
  max-height: 138px;
  background: #fff;
}

.gallery-tab {
  border: 1px solid #cfdaee;
  border-radius: 5px;
  background: #f6f9ff;
  color: #334155;
  padding: 7px 10px;
  font: inherit;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}

.gallery-tab.is-active {
  background: #4f883d;
  border-color: #4f883d;
  color: #fff;
}

.gallery-grid {
  padding: 12px 14px 14px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
  gap: 10px;
  overflow: auto;
}

.gallery-item {
  border: 1px solid #d7e1ef;
  border-radius: 5px;
  background: #fff;
  aspect-ratio: 1;
  padding: 7px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.gallery-item:hover {
  transform: translateY(-1px);
  border-color: #8db9ff;
  box-shadow: 0 6px 18px rgba(35, 94, 196, 0.2);
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.ai-modal-dialog {
  width: min(640px, calc(100vw - 40px));
}

.ai-form {
  padding: 12px 14px 14px;
  overflow: auto;
}

.ai-lang-fieldset {
  margin: 2px 0 8px;
  border: 1px solid #d6e0ef;
  border-radius: 5px;
  padding: 8px 10px 10px;
}

.ai-lang-fieldset .field-label {
  margin: 0 0 6px;
}

.ai-lang-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}

.ai-lang-grid label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.95rem;
  color: #1f2937;
}

.ai-lang-grid input[type='radio'] {
  width: auto;
  margin: 0;
}

.ai-actions {
  margin-top: 10px;
}

.ai-error {
  margin-top: 4px;
  color: #b42318;
  font-weight: 700;
}

.print-footer {
  display: none;
}

@media (max-width: 1150px) {
  .app-shell {
    grid-template-columns: 320px 1fr;
  }
}

@media (max-width: 840px) {
  .app-shell {
    grid-template-columns: 1fr;
    padding: 14px;
  }

  .settings-panel {
    position: static;
    max-height: none;
  }

  .workspace {
    gap: 10px;
  }

  .card-top {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 620px) {
  .field-group {
    grid-template-columns: 1fr;
  }

  .symbol-grid {
    grid-template-columns: 1fr 1fr;
  }

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

  .upload-row .gallery-pick-btn,
  .upload-row input[type='file'] {
    grid-column: 1 / -1;
  }

  .upload-row .upload-thumb {
    grid-column: 2;
    justify-self: end;
  }

  .sudoku-board {
    width: 100%;
  }

  .gallery-modal-dialog {
    width: calc(100vw - 20px);
    max-height: calc(100vh - 20px);
    margin: 10px auto;
  }

  .ai-lang-grid {
    grid-template-columns: 1fr;
  }
}

@media print {
  @page {
    margin: 12mm;
  }

  body {
    background: #fff;
    padding: 0 0 14mm;
  }

  .app-shell {
    display: block;
    padding: 0;
  }

  .settings-panel,
  .chip,
  .gallery-modal {
    display: none;
  }

  .card,
  .preview-panel,
  .preview-canvas {
    border: 0;
    box-shadow: none;
    padding: 0;
    background: #fff;
  }

  .print-footer {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 4mm;
    text-align: center;
    font-size: 10.5pt;
    color: #475569;
  }
}
