:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --border: #e3e6eb;
  --border-strong: #c8cdd5;
  --text: #1f2937;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --row-h: 32px;
  --cell-w: 44px;
  --label-w: 140px;
  --no-w: 48px;
  --actions-w: 80px;
  --shadow-1: 0 1px 2px rgba(0,0,0,.04), 0 1px 3px rgba(0,0,0,.06);
  --shadow-2: 0 8px 24px rgba(0,0,0,.12);
  --t: 200ms ease-out;
  --header-h: 48px;
  --period-h: 56px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 13px; line-height: 1.4;
  overflow: hidden; /* B-1: 세로 스크롤 금지 */
}
a { color: inherit; text-decoration: none; }

/* Topbar */
.topbar {
  height: var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.logo { font-weight: 700; font-size: 15px; }
.topnav a {
  display: inline-block; padding: 6px 12px; border-radius: 6px;
  transition: background var(--t);
}
.topnav a:hover { background: var(--bg); }
.topnav a.active { background: var(--primary); color: white; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  height: calc(100vh - var(--header-h));
}
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head {
  padding: 12px 14px; font-weight: 600; color: var(--text-muted);
  font-size: 12px; letter-spacing: .04em; border-bottom: 1px solid var(--border);
}
.tabs { overflow-y: auto; flex: 1; padding: 6px; }
.tab {
  display: block; padding: 8px 12px; border-radius: 6px;
  cursor: pointer; transition: background var(--t);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab:hover { background: var(--bg); }
.tab.active { background: var(--primary); color: white; }

.content { min-height: 0; min-width: 0; display: flex; flex-direction: column; }

/* Period bar */
.period-bar {
  height: var(--period-h); padding: 0 16px;
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.period-bar label { font-size: 12px; color: var(--text-muted); }
.period-bar select, .period-bar input {
  height: 32px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: 6px; background: white; font: inherit;
}
.spacer { flex: 1; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 14px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border-strong); background: white;
  font: inherit; transition: background var(--t), border-color var(--t);
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: var(--border-strong); }
.btn.danger:hover { background: #fef2f2; border-color: var(--danger); }
.btn.icon { width: 28px; height: 28px; padding: 0; border-color: transparent; background: transparent; }
.btn.icon:hover { background: var(--bg); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Subtabs */
.subtabs {
  display: flex; gap: 2px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.subtab {
  background: transparent; border: none; cursor: pointer;
  padding: 10px 18px; font: inherit; color: var(--text-muted);
  border-bottom: 2px solid transparent;
  transition: color var(--t), border-color var(--t);
}
.subtab:hover { color: var(--text); }
.subtab.active {
  color: var(--primary); border-bottom-color: var(--primary);
  font-weight: 600;
}
.matrix-card.hidden { display: none; }

/* Tables wrapper for matrices */
.matrices {
  flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 16px 16px;
  display: flex; flex-direction: column; gap: 14px; min-height: 0;
}
.matrix-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow-1); overflow: hidden;
  flex-shrink: 0; min-width: 0;
}
.matrix-head {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.matrix-head .badge {
  font-size: 11px; color: var(--text-muted); font-weight: 500;
  padding: 2px 8px; border: 1px solid var(--border); border-radius: 999px;
}

.scroll-x {
  overflow-x: auto; overflow-y: hidden;
}

/* Matrix table */
.matrix {
  border-collapse: separate; border-spacing: 0;
  white-space: nowrap;
  table-layout: fixed; min-width: 100%;
}
.matrix th, .matrix td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: center; padding: 0;
  height: var(--row-h);
  background: white;
}
.matrix thead th {
  position: sticky; top: 0; z-index: 3;
  background: #f8fafc; font-weight: 600; font-size: 12px;
  color: var(--text);
}
.matrix thead th.day-h { font-weight: 600; }
.matrix thead th.subhead { font-weight: 400; color: var(--text-muted); }

.matrix th.no-col, .matrix td.no-col   { width: var(--no-w); background: #f8fafc; }
.matrix th.label-col, .matrix td.label-col { width: var(--label-w); background: #f8fafc; text-align: left; padding: 0 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.matrix tbody td.label-col { background: white; font-weight: 500; }
.matrix tbody td.no-col   { background: white; color: var(--text-muted); }
.matrix th.day-h { width: calc(var(--cell-w) * 2); }
.matrix th.subhead, .matrix td.cell { width: var(--cell-w); }
.matrix td.sum  { background: #f8fafc; font-weight: 600; }
/* Actions sticky to right so 수정/삭제가 항상 보임 */
.matrix th.actions, .matrix td.actions {
  width: var(--actions-w);
  position: sticky; right: 0; z-index: 4;
  background: #f8fafc;
  box-shadow: -1px 0 0 var(--border);
}
.matrix tbody td.actions { background: white; }
.matrix thead th.actions { z-index: 5; } /* corner pin above body */

.cell-input {
  border: none; background: transparent; width: 100%; height: 100%;
  text-align: center; font: inherit; padding: 0; outline: none;
}
.cell-input:focus { box-shadow: inset 0 0 0 2px var(--primary); }
.cell-input::selection { background: #bfdbfe; }
.cell-input.dirty { background: #fffbeb; }

/* Hide spinner per B-2 */
.cell-input[type=number]::-webkit-inner-spin-button,
.cell-input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.cell-input[type=number] { -moz-appearance: textfield; }

.matrix tr.totals td { background: #f1f5f9; font-weight: 600; color: var(--text); }
.matrix tr.totals td.label-col, .matrix tr.totals td.no-col,
.matrix tr.totals td.actions { background: #f1f5f9; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; opacity: 0; transition: opacity var(--t);
}
.modal-backdrop.show { opacity: 1; }
.modal {
  background: white; border-radius: 10px; min-width: 440px; max-width: 92vw;
  max-height: 88vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow-2);
  transform: translateY(8px); opacity: 0;
  transition: transform var(--t), opacity var(--t);
}
.modal-backdrop.show .modal { transform: translateY(0); opacity: 1; }
.modal-head {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: 15px;
  display: flex; justify-content: space-between; align-items: center;
}
.modal-body { padding: 20px; overflow: auto; }
.modal-foot {
  padding: 12px 20px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field:last-child { margin-bottom: 0; }
.field > label { font-size: 12px; color: var(--text-muted); font-weight: 500; }
.field input, .field select {
  height: 36px; padding: 0 12px; border: 1px solid var(--border-strong);
  border-radius: 6px; font: inherit; color: var(--text);
  transition: border-color var(--t), box-shadow var(--t);
}
.field input:focus, .field select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.field input::placeholder { color: #9ca3af; }
.field .radio-row { display: flex; gap: 8px; padding: 2px 0; }
.field .radio-row label {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px; flex: 1; justify-content: center;
  border: 1px solid var(--border-strong); border-radius: 6px;
  cursor: pointer; font-size: 13px; color: var(--text);
  background: white; user-select: none;
  transition: background var(--t), border-color var(--t), color var(--t);
}
.field .radio-row label:hover { background: var(--bg); }
.field .radio-row input[type=radio] { margin: 0; accent-color: var(--primary); }
.field .radio-row label:has(input:checked) {
  background: #eff6ff; border-color: var(--primary); color: var(--primary);
  font-weight: 600;
}

/* Day grid in modal */
.day-grid-cols {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px 24px;
  min-width: 520px;
}
.day-grid {
  display: grid; grid-template-columns: 40px repeat(2, 1fr); gap: 6px 8px;
  align-items: center;
}
.day-grid .gh { font-size: 11px; color: var(--text-muted); text-align: center; font-weight: 500; }
.day-grid input {
  height: 28px; padding: 0 6px; border: 1px solid var(--border);
  border-radius: 4px; text-align: center; font: inherit;
  transition: border-color var(--t), box-shadow var(--t);
}
.day-grid input:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
}
.day-grid .day-label { color: var(--text-muted); font-size: 12px; text-align: right; }
.day-grid .day-label.disabled { color: #cbd5e1; }
.day-grid input:disabled {
  background: #f1f5f9; color: #cbd5e1; cursor: not-allowed;
  border-color: #e2e8f0;
}
.day-grid .total-label {
  font-weight: 600; color: var(--text); font-size: 12px;
  border-top: 1px solid var(--border); padding-top: 4px; margin-top: 2px;
}
.day-grid .total-cell {
  height: 28px; display: flex; align-items: center; justify-content: center;
  font-weight: 600; color: var(--primary);
  background: #eff6ff; border: 1px solid #bfdbfe; border-radius: 4px;
  border-top-width: 1px;
  margin-top: 2px;
}

/* Hide number spinners globally — direct entry only */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

/* Standard table for admin */
.table-wrap { padding: 16px; flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; }
.table-toolbar { display: flex; align-items: center; gap: 8px; }
.std-table-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: var(--shadow-1); overflow: hidden;
  flex: 1; display: flex; flex-direction: column; min-height: 0;
}
.std-table {
  width: 100%; border-collapse: separate; border-spacing: 0;
  white-space: nowrap; table-layout: fixed;
}
.std-table th, .std-table td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 0 10px; height: 40px; text-align: center;
  overflow: hidden; text-overflow: ellipsis;
}
.std-table thead th {
  position: sticky; top: 0; background: #f8fafc; font-weight: 600;
}
.std-table tbody tr:hover td { background: #fafbfc; }
.std-table .col-actions { width: 88px; }
.std-table .col-state   { width: 64px; }

.scrollbody { overflow: auto; flex: 1; }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 36px; height: 20px; }
.switch input { display: none; }
.switch span {
  position: absolute; inset: 0; background: #cbd5e1; border-radius: 999px;
  transition: background var(--t);
}
.switch span::after {
  content: ""; position: absolute; left: 2px; top: 2px;
  width: 16px; height: 16px; background: white; border-radius: 50%;
  transition: transform var(--t);
}
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(16px); }

/* Toast */
.toast-root {
  position: fixed; bottom: 20px; right: 20px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: #1f2937; color: white; padding: 10px 14px; border-radius: 6px;
  box-shadow: var(--shadow-2); font-size: 13px;
  transform: translateY(8px); opacity: 0;
  transition: transform var(--t), opacity var(--t);
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.error { background: var(--danger); }

/* Skeleton */
.skeleton {
  background: linear-gradient(90deg, #eef0f4 25%, #e3e6eb 50%, #eef0f4 75%);
  background-size: 200% 100%; animation: sk 1.2s linear infinite;
  border-radius: 4px;
}
@keyframes sk { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.empty {
  display: flex; align-items: center; justify-content: center;
  padding: 40px; color: var(--text-muted);
}
