:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #d8dee6;
  --text: #172033;
  --muted: #667085;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --soft: #eef2f6;
  --work-area-height: 560px;
  --review-empty-height: 380px;
  --review-loaded-height: max(620px, calc(100vh - 310px));
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Malgun Gothic", "Segoe UI", sans-serif;
  font-size: 14px;
}

body.login-mode {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #ffffff;
}

.shell {
  width: min(1080px, 100%);
  margin: 0 auto;
  padding: 14px;
}

body.login-mode .shell {
  width: 360px;
  min-height: 360px;
  display: grid;
  place-items: center;
  padding: 0;
}

body.login-mode .topbar,
body.login-mode #statusBar {
  display: none;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 2px 8px;
  margin-bottom: 4px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 22px; letter-spacing: 0; }
h2 { font-size: 17px; margin-bottom: 10px; }
p { color: var(--muted); margin-top: 4px; }

.account { display: flex; gap: 8px; align-items: center; }

.panel { padding: 14px; margin-bottom: 10px; }

.login-panel {
  width: min(320px, calc(100vw - 32px));
  min-height: 320px;
  max-height: 360px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 14px;
}

.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.login-brand img {
  width: 118px;
  height: 142px;
  object-fit: contain;
  display: block;
}

.login-brand strong {
  font-size: 29px;
  font-weight: 800;
  letter-spacing: 0;
  line-height: 1;
  color: #162033;
}

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

.login-grid input,
.login-grid button {
  width: 100%;
  height: 38px;
}

.upload-panel,
.title-panel,
.section-head,
.actions,
.tab-actions,
.pdf-panel,
.pdf-controls,
.pdf-actions,
.cover-row,
.settings-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.upload-panel {
  justify-content: flex-start;
  border-style: dashed;
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
  min-height: 78px;
}
.upload-panel.drag-over {
  background: #eef6ff;
  border-color: var(--blue);
  box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.22);
}
.upload-copy {
  min-width: 0;
  flex: 1;
}
.upload-copy p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-button-tall {
  width: 54px;
  min-height: 50px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.2;
  padding: 6px 8px;
}
.pdf-panel {
  justify-content: space-between;
  gap: 12px;
}
.pdf-panel h2 {
  flex: 0 0 auto;
  margin-bottom: 0;
}
.pdf-controls {
  justify-content: flex-end;
  flex: 1;
  min-width: 0;
  flex-wrap: wrap;
}
.cover-row {
  justify-content: flex-end;
  font-weight: 700;
}
.cover-row span { white-space: nowrap; }
.cover-row select { min-width: 220px; }
.pdf-actions { flex-wrap: nowrap; justify-content: flex-end; }
.title-panel label { font-weight: 700; width: 46px; }
.title-panel input { flex: 1; }
.section-head {
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
}
.section-head h2 { margin-bottom: 0; }
.actions { flex-wrap: wrap; justify-content: flex-end; }
.tab-actions {
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

input, select, textarea, button {
  font-family: inherit;
  font-size: 14px;
}

input, select, textarea {
  border: 1px solid #cbd5df;
  border-radius: 6px;
  background: white;
  padding: 8px 9px;
  color: var(--text);
}

button,
.file-button {
  border: 1px solid var(--blue);
  background: var(--blue);
  color: white;
  border-radius: 6px;
  padding: 8px 11px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover,
.file-button:hover { background: var(--blue-dark); }

button.secondary {
  background: var(--soft);
  border-color: #cbd5df;
  color: var(--text);
}

button.ghost {
  background: transparent;
  border-color: #cbd5df;
  color: var(--text);
}

.file-button input { display: none; }

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  margin: 10px 0 0;
  padding: 0;
  background: transparent;
  border-bottom: 1px solid var(--border);
  overflow: visible;
}

.tab {
  background: transparent;
  border-color: transparent;
  color: #475467;
  border-radius: 7px 7px 0 0;
  padding: 10px 20px;
  font-weight: 700;
  position: relative;
  margin-bottom: -1px;
}

.tab.active {
  background: var(--panel);
  border-color: var(--border);
  border-bottom-color: var(--panel);
  color: var(--blue);
  box-shadow: none;
  transform: none;
}

.tab:hover {
  background: #f7f9fc;
  color: var(--text);
}

.tab.active:hover {
  background: var(--panel);
  color: var(--blue);
}

.tab-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 8px 8px;
  padding: 10px;
  margin-bottom: 10px;
}

.tab-panel > .panel:last-child,
.tab-panel > .grid:last-child,
.tab-panel > .review-grid:last-child {
  margin-bottom: 0;
}

.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 10px;
}

.review-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 10px;
}

.table-wrap {
  overflow: auto;
  max-height: var(--work-area-height);
  border: 1px solid var(--border);
  border-radius: 6px;
}

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 760px;
  background: white;
}

th, td {
  border-bottom: 1px solid #e1e7ef;
  border-right: 1px solid #e1e7ef;
  padding: 7px;
  text-align: left;
}

th {
  background: #eef2f6;
  font-weight: 700;
  position: sticky;
  top: 0;
}

td[contenteditable="true"] { background: #fff; }
td:focus { outline: 2px solid #93c5fd; outline-offset: -2px; }
tr.selected-row td { background: #eff6ff; }

.select-cell {
  padding: 4px;
}

.cell-select {
  width: 100%;
  min-width: 112px;
  height: 32px;
  padding: 4px 6px;
  border-radius: 4px;
}

.sheet-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.review-result-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.review-results {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: var(--review-empty-height);
  min-height: var(--review-empty-height);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  background: #fbfcfe;
}

.review-results.empty {
  justify-content: center;
  align-items: center;
  color: var(--muted);
  text-align: center;
}

.review-item {
  width: 100%;
  display: grid;
  gap: 5px;
  text-align: left;
  white-space: normal;
  color: var(--text);
  background: #ffffff;
  border-color: var(--border);
  padding: 10px;
}

.review-item:hover {
  background: #f8fbff;
  border-color: #b9d3ff;
}

.review-item.active {
  background: #eff6ff;
  border-color: var(--blue);
  box-shadow: inset 3px 0 0 var(--blue);
}

.review-item .review-meta {
  color: var(--blue);
  font-weight: 700;
  font-size: 12px;
}

.review-item strong {
  font-size: 13px;
  line-height: 1.45;
}

.review-item small {
  color: var(--muted);
  line-height: 1.4;
}

#reviewText {
  width: 100%;
  height: var(--review-empty-height);
  min-height: var(--review-empty-height);
  resize: vertical;
  line-height: 1.55;
}

#reviewTab.review-loaded #reviewText,
#reviewTab.review-loaded .review-results {
  height: var(--review-loaded-height);
  min-height: var(--review-loaded-height);
}

.settings-row { margin-bottom: 10px; }
.settings-row input[type="password"] { min-width: 360px; }

.message { min-height: 20px; color: var(--muted); }
.hidden { display: none !important; }

.omr-shell {
  width: min(1420px, 100%);
}

.scan-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 10px;
}

.omr-drop-zone {
  min-height: 84px;
}

.omr-results-panel {
  min-width: 0;
}

.omr-table-wrap {
  max-height: calc(100vh - 245px);
  min-height: 520px;
}

.omr-table {
  min-width: 1180px;
}

.omr-table th:nth-child(1) { width: 58px; }
.omr-table th:nth-child(2),
.omr-table th:nth-child(3) { width: 92px; }
.omr-table th:nth-child(4) { width: 70px; }
.omr-table th:nth-child(5) { width: 310px; }
.omr-table th:nth-child(6) { width: 82px; }
.omr-table th:nth-child(7) { width: 250px; }
.omr-table th:nth-child(8) { width: 280px; }

.omr-table tr.needs-review td {
  background: #fff4e8;
}

.answer-chip-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  max-height: 92px;
  overflow: auto;
}

.answer-chip {
  padding: 3px 5px;
  min-width: 42px;
  height: 24px;
  border-radius: 4px;
  font-size: 12px;
  background: #eef2f6;
  border-color: #cbd5df;
  color: var(--text);
}

.answer-chip:hover {
  background: #e0ecff;
  border-color: #93c5fd;
}

.answer-chip.bad {
  background: #ffe8d5;
  border-color: #f3b27a;
  color: #8a3b0c;
}

.sheet-select {
  width: 100%;
  min-width: 260px;
}

.omr-preview-panel {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.preview-canvas {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fbfcfe;
  min-height: 520px;
  overflow: auto;
  display: grid;
  place-items: start center;
  padding: 8px;
}

.preview-canvas.empty {
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.preview-stage {
  position: relative;
  width: 100%;
  min-width: 320px;
}

.preview-stage img {
  width: 100%;
  display: block;
}

.preview-marker {
  position: absolute;
  border: 3px solid #ef4444;
  background: rgba(239, 68, 68, 0.10);
  pointer-events: none;
}

.preview-meta {
  min-height: 34px;
  padding-top: 8px;
  color: var(--muted);
  line-height: 1.4;
}

.placeholder-box {
  min-height: 280px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  display: grid;
  place-items: center;
  color: var(--muted);
  background: #fbfcfe;
}

#statusBar {
  color: #475467;
  padding: 4px 2px;
}

@media (max-width: 900px) {
  .grid, .review-grid { grid-template-columns: 1fr; }
  .upload-panel, .section-head { align-items: stretch; flex-direction: column; }
  .pdf-panel, .pdf-controls { align-items: stretch; flex-direction: column; }
  .cover-row, .pdf-actions { justify-content: stretch; }
  .pdf-actions { flex-wrap: wrap; }
  .file-button-tall { width: 100%; flex-direction: row; gap: 4px; }
  .upload-copy p { white-space: normal; }
  .scan-layout { grid-template-columns: 1fr; }
  .preview-canvas { min-height: 360px; }
  .omr-table-wrap { min-height: 360px; }
}
