:root {
  --bg: #101216;
  --panel: #181b22;
  --panel-2: #20242e;
  --text: #eef2f7;
  --muted: #9aa5b5;
  --line: #343a46;
  --accent: #75d6b3;
  --accent-2: #8db7ff;
  --danger: #ff7b7b;
  --paper: #fffdf8;
  --paper-ink: #171717;
  --grid: #161616;
  --shadow: 0 18px 80px rgba(0, 0, 0, 0.35);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top left, rgba(117, 214, 179, 0.08), transparent 34rem), var(--bg);
  color: var(--text);
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto auto;
  gap: 20px;
  align-items: center;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(16, 18, 22, 0.94);
  backdrop-filter: blur(18px);
}

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

.brand-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 24px rgba(117, 214, 179, 0.35);
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
}

.brand p,
.section-head p,
.viewer-head p {
  color: var(--muted);
  margin-top: 3px;
}

.main-tabs,
.segmented,
.auth-box,
.panel-actions,
.viewer-actions,
.modal-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.segmented {
  flex-wrap: wrap;
}

.segment {
  padding-inline: 12px;
}

.tab,
.segment,
.ghost-button,
.secondary-button,
.primary-button,
.danger-button,
.file-button,
.icon-button,
.stepper button {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel-2);
  min-height: 38px;
  padding: 0 13px;
  transition: transform 120ms ease, filter 120ms ease, box-shadow 120ms ease, border-color 120ms ease;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.22);
}

.tab.is-active,
.segment.is-active,
.primary-button {
  border-color: rgba(87, 230, 160, 0.75);
  background: linear-gradient(180deg, #2fbe78, #1d7d56);
  color: #f8fff9;
}

.secondary-button {
  background: linear-gradient(180deg, #477df0, #2d4f9b);
  border-color: rgba(141, 183, 255, 0.65);
  color: #f8fbff;
}

.danger-button {
  background: linear-gradient(180deg, #ff7368, #b33a3f);
  border-color: rgba(255, 155, 145, 0.75);
  color: #fffafa;
}

.ghost-button {
  background: linear-gradient(180deg, #343a47, #20242e);
  border-color: rgba(255, 255, 255, 0.12);
}

button:hover,
.file-button:hover {
  filter: brightness(1.08);
  transform: translateY(-1px);
}

button:active,
.file-button:active {
  filter: brightness(0.96);
  transform: translateY(1px);
  box-shadow: inset 0 2px 0 rgba(0, 0, 0, 0.28);
}

.panel-actions {
  flex-wrap: wrap;
}

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

.panel-actions .primary-button,
.panel-actions .secondary-button,
.panel-actions .ghost-button,
.panel-actions .danger-button {
  flex: 1 1 96px;
  min-height: 34px;
  padding: 0 9px;
  font-size: 13px;
}

button:disabled,
button.is-disabled {
  cursor: default;
  opacity: 0.45;
  filter: grayscale(0.35);
  transform: none;
}

.icon-button {
  width: 38px;
  padding: 0;
  font-size: 20px;
}

main {
  padding: 24px;
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: end;
  margin-bottom: 18px;
}

.search-input,
input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0f1116;
  color: var(--text);
  padding: 10px 12px;
}

.search-input {
  max-width: 360px;
}

textarea {
  resize: vertical;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.cards-grid.compact {
  grid-template-columns: 1fr;
}

.library-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.library-list {
  grid-template-columns: 1fr;
  align-content: start;
  max-height: calc(100vh - 155px);
  overflow: auto;
  padding-right: 4px;
}

.quiz-view-panel {
  min-width: 0;
  display: grid;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
  box-shadow: var(--shadow);
}

.preview-sheets {
  display: grid;
  gap: 14px;
  max-height: calc(100vh - 230px);
  overflow: auto;
  padding-right: 4px;
}

.preview-sheets .sheet {
  width: 100%;
  max-width: 270mm;
  height: clamp(240px, calc((100vw - 390px) * 0.63), 170mm);
  aspect-ratio: 270 / 170;
  margin: 0 auto;
  flex: 0 0 auto;
}

.quiz-view-panel .viewer-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.quiz-card,
.user-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
  box-shadow: var(--shadow);
}

.library-list .quiz-card {
  cursor: pointer;
  box-shadow: none;
}

.library-list .quiz-card.is-selected {
  border-color: rgba(117, 214, 179, 0.85);
  background: #1d2a28;
  outline: 2px solid rgba(117, 214, 179, 0.28);
}

.quiz-card h3 {
  font-size: 17px;
  margin-top: 10px;
}

.card-meta,
.card-actions,
.status-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.card-meta {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.card-actions {
  margin-top: 12px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  background: #252a34;
  color: var(--muted);
  font-size: 12px;
}

.status-pill.published {
  color: #baf7dc;
  background: #193c31;
}

.status-pill.pending {
  color: #d6e3ff;
  background: #263451;
}

.status-pill.rejected {
  color: #ffc7c7;
  background: #44272a;
}

.comment-tip {
  border-bottom: 1px dotted currentColor;
}

.editor-view.is-active {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 14px 22px;
  align-items: start;
}

.editor-top {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: minmax(260px, 1.4fr) minmax(150px, 0.75fr) minmax(170px, 0.75fr) auto;
  gap: 10px;
  align-items: center;
}

.editor-top-actions {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  justify-content: flex-end;
}

.editor-top input {
  min-height: 38px;
}

.big-action {
  min-height: 44px;
  padding: 0 18px;
  font-weight: 800;
}

.editor-panel {
  position: sticky;
  top: 82px;
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 16px;
}

.panel-block,
.cell-editor,
.panel-row label {
  display: grid;
  gap: 8px;
}

.panel-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label span,
.label {
  color: var(--muted);
  font-size: 13px;
}

.cell-editor-head,
.font-row,
.sheet-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.stepper {
  display: grid;
  grid-template-columns: 38px 42px 38px;
  align-items: center;
  text-align: center;
}

.stepper output {
  color: var(--text);
}

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

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

.file-button {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 38px;
  background: linear-gradient(180deg, #477df0, #2d4f9b);
  border-color: rgba(141, 183, 255, 0.65);
  color: #f8fbff;
  font-weight: 700;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.sheet-stage {
  min-width: 0;
  max-height: calc(100vh - 132px);
  overflow: auto;
  padding-right: 6px;
}

.sheet-toolbar {
  max-width: 270mm;
  margin: 0 auto 12px;
  color: var(--muted);
}

.editor-sheets {
  display: grid;
  gap: 0;
  color: var(--paper-ink);
}

.editor-sheet-block {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: #f6f2e9;
}

.editor-sheet-block.is-active {
  border-color: rgba(117, 214, 179, 0.7);
  box-shadow: 0 0 0 2px rgba(117, 214, 179, 0.16);
}

.editor-sheet-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: #2c2c2c;
  font-size: 13px;
}

.editor-sheet-head span {
  color: #666;
}

.editor-grid-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: min(62vh, 520px);
}

.editor-grid-pair .paper-grid {
  min-height: 100%;
  background: #fffefa;
}

.sheet-divider {
  height: 14px;
  background: #4b515b;
  border-top: 1px solid #686f7a;
  border-bottom: 1px solid #30343b;
}

.sheet,
.sheet-preview {
  color: var(--paper-ink);
  background: var(--paper);
}

.sheet {
  width: 270mm;
  height: 170mm;
  margin: 0 auto;
  padding: 6mm;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 13mm 1fr;
  gap: 0;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.sheet.is-empty-preview,
.preview-sheets.is-empty-preview {
  display: grid;
  place-items: center;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  min-height: 240px;
  color: #8d8372;
  font-weight: 700;
}

#printArea {
  display: none;
}

.sheet-info {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 8px;
  color: #333;
  font-size: 10pt;
  padding-bottom: 3mm;
}

.sheet-info strong {
  display: block;
  font-size: 14pt;
}

.paper-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  border-top: 2px solid var(--grid);
  border-left: 2px solid var(--grid);
}

.paper-grid.left {
  border-right: 1px solid var(--grid);
}

.paper-grid.right {
  border-left-width: 1px;
  border-right: 2px solid var(--grid);
}

.paper-cell {
  position: relative;
  overflow: hidden;
  border-right: 2px solid var(--grid);
  border-bottom: 2px solid var(--grid);
  background: #fffefa;
}

.paper-cell.is-selected {
  outline: 3px solid rgba(117, 214, 179, 0.75);
  outline-offset: -5px;
}

.paper-cell.is-linked {
  background: #effaf5;
}

.cell-content {
  position: absolute;
  inset: 2.6mm 2.6mm 6.2mm 2.6mm;
  text-align: center;
  line-height: 1.05;
  overflow: hidden;
  font-weight: 700;
}

.cell-content.has-text {
  display: block;
}

.cell-content.has-image {
  inset: 1.5mm 1.5mm 6.2mm 1.5mm;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.cell-content img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: top center;
}

.touch-avoid {
  float: left;
  width: 14mm;
  height: 100%;
  shape-outside: polygon(0 67%, 100% 67%, 100% 100%, 0 100%);
  clip-path: polygon(0 67%, 100% 67%, 100% 100%, 0 100%);
}

.cell-text-value {
  display: block;
}

.press-mark {
  position: absolute;
  left: -7.5mm;
  bottom: -7.5mm;
  width: 19mm;
  height: 19mm;
  border: 1.5px solid #222;
  border-radius: 50%;
  background: transparent;
  z-index: 2;
  pointer-events: none;
}

.selection-star {
  position: absolute;
  left: 3.2mm;
  bottom: 2.2mm;
  z-index: 4;
  display: none;
  color: #ffca35;
  font-size: 17pt;
  line-height: 1;
  pointer-events: none;
  text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff, 0 2px 6px rgba(0, 0, 0, 0.35);
}

.paper-cell.is-selected .selection-star,
.paper-cell.is-linked .selection-star {
  display: block;
}

.paper-cell.is-selected .selection-star {
  color: #ffb800;
  transform: scale(1.08);
}

.paper-cell.is-linked .selection-star {
  color: rgba(255, 184, 0, 0.55);
  transform: scale(0.92);
}

.cell-code {
  display: none;
}

.library-preview {
  aspect-ratio: 2 / 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  padding: 6px;
  border-radius: 6px;
  background: #eee9dc;
}

.preview-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 1px;
}

.preview-cell {
  background: #fffefa;
  color: #111;
  font-size: 8px;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.preview-cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.modal,
.viewer-modal {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}

.modal::backdrop,
.viewer-modal::backdrop {
  background: rgba(0, 0, 0, 0.72);
}

dialog:not([open]) {
  display: none !important;
}

.modal {
  width: min(440px, calc(100vw - 24px));
}

.modal.wide {
  width: min(860px, calc(100vw - 24px));
}

.modal form,
.viewer-modal {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.modal-close {
  justify-self: end;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: #2a2f3a;
  color: var(--text);
  font-size: 20px;
}

.form-error {
  min-height: 18px;
  color: var(--danger);
}

.viewer-modal {
  width: min(96vw, 1180px);
}

.viewer-head {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: start;
}

.admin-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.8fr);
  gap: 18px;
}

.user-list {
  display: grid;
  gap: 10px;
}

.user-row {
  display: grid;
  gap: 8px;
}

.mini-sheet {
  transform: scale(0.48);
  transform-origin: top left;
  margin: 0;
}

.admin-preview {
  height: 330px;
  overflow: auto;
  background: #0f1116;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.hidden,
.auth-only.hidden,
.admin-only.hidden {
  display: none !important;
}

@media (max-width: 980px) {
  .app-header {
    grid-template-columns: 1fr;
  }

  .main-tabs {
    overflow-x: auto;
  }

  .editor-top {
    grid-template-columns: 1fr;
  }

  .editor-top-actions {
    justify-content: stretch;
  }

  .editor-top-actions button {
    flex: 1;
  }

  .editor-view.is-active,
  .library-layout,
  .admin-layout {
    grid-template-columns: 1fr;
  }

  .library-list {
    max-height: none;
  }

  .editor-panel {
    position: static;
  }

  .sheet {
    width: min(100%, 270mm);
    aspect-ratio: 270 / 170;
    height: auto;
  }

  .preview-sheets .sheet {
    height: clamp(190px, calc((100vw - 72px) * 0.63), 170mm);
  }
}

@media print {
  body {
    background: white;
  }

  body > *:not(#printArea) {
    display: none !important;
  }

  #printArea {
    display: block !important;
    padding: 0;
  }

  #printArea .sheet {
    display: grid !important;
    width: 270mm;
    height: 170mm;
    margin: 0;
    box-shadow: none;
    page-break-inside: avoid;
  }

  #printArea .sheet:not(:last-child) {
    page-break-after: always;
  }

  @page {
    size: A4 landscape;
    margin: 12mm;
  }
}
