:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --bg: #f8fafc;
  --text: #1e293b;
  --text-light: #64748b;
  --guide: #e2e8f0;
  --paper-bg: #ffffff;
  --cell-w: 1.4em;
  --cell-h: 1.6em;
  --color-work: #2563eb; 
  --color-ans: #e11d48;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Pretendard', sans-serif;
  background: var(--bg); color: var(--text); line-height: 1; padding-bottom: 50px;
}

/* UI Controls */
.controls {
  background: var(--paper-bg); padding: 20px; max-width: 1000px; margin: 20px auto; border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; justify-content: center;
}
.controls h1 { width: 100%; text-align: center; font-size: 22px; font-weight: 800; margin-bottom: 8px; color: var(--primary); }
.grp { display: flex; flex-direction: column; gap: 2px; }
.grp label { font-size: 12px; font-weight: 600; color: var(--text-light); }
.grp select, .grp input { padding: 6px 10px; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 13px; }
.grp-hint { font-size: 10px; color: #94a3b8; line-height: 1.3; min-height: 1.3em; }
.grp > .btn, .grp > .btn-help, .grp > .btn-help-icon { align-self: flex-start; }
.btn { padding: 8px 20px; border: none; border-radius: 6px; font-size: 13px; font-weight: 700; cursor: pointer; background: var(--primary); color: white; }
.btn:hover { background: var(--primary-dark); }
.btn-outline { background: transparent; color: var(--primary); border: 2px solid var(--primary); padding: 6px 18px; border-radius: 8px; font-size: 13px; font-weight: 700; cursor: pointer; transition: all 0.15s; }
.btn-outline:hover { background: var(--primary); color: white; }

/* Help Button */
.btn-help {
  padding: 8px 16px; border: 2px solid #94a3b8; border-radius: 8px;
  background: white; color: #64748b; font-size: 13px; font-weight: 700;
  cursor: pointer; transition: all 0.15s; white-space: nowrap;
}
.btn-help:hover { background: #f1f5f9; color: var(--text); border-color: #64748b; }

.btn-help-icon {
  width: 36px; height: 36px; border: 2px solid #cbd5e1; border-radius: 50%;
  background: white; color: #94a3b8; font-size: 16px; font-weight: 800;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.btn-help-icon:hover { border-color: #94a3b8; color: #64748b; background: #f8fafc; }

/* Help Modal */
.modal-overlay {
  display: none; position: fixed; inset: 0; z-index: 1000;
  background: rgba(15,23,42,0.5); backdrop-filter: blur(6px);
  align-items: center; justify-content: center;
  animation: fadeIn 0.2s ease;
}
.modal-overlay.active { display: flex; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-box {
  background: white; border-radius: 20px; padding: 40px;
  max-width: 460px; width: 92%; position: relative;
  box-shadow: 0 25px 80px rgba(0,0,0,0.2);
  animation: slideUp 0.25s ease;
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-close {
  position: absolute; top: 16px; right: 16px;
  width: 32px; height: 32px; border: none; background: #f1f5f9; border-radius: 50%;
  font-size: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  color: #64748b; transition: all 0.15s;
}
.modal-close:hover { background: #e2e8f0; color: var(--text); }
.modal-box h2 { font-size: 22px; font-weight: 800; margin-bottom: 6px; color: var(--text); }
.modal-sub { font-size: 14px; color: var(--text-light); margin-bottom: 24px; }
.modal-section { margin-bottom: 20px; }
.modal-section h3 { font-size: 14px; font-weight: 700; margin-bottom: 6px; display: flex; align-items: center; gap: 6px; }
.modal-section p { font-size: 13.5px; color: var(--text-light); line-height: 1.7; }
kbd {
  display: inline-block; padding: 1px 5px; font-size: 12px; font-family: inherit;
  background: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 4px;
  box-shadow: 0 1px 0 #cbd5e1; line-height: 1.4;
}

/* Worksheet */
.sheet {
  background: var(--paper-bg); width: 210mm; min-height: 297mm; margin: 0 auto 30px; padding: 15mm;
  box-shadow: 0 4px 10px rgba(0,0,0,0.05); position: relative;
  display: flex; flex-direction: column;
}
.shdr { height: 40px; display: flex; justify-content: space-between; align-items: flex-end; border-bottom: 2px solid var(--text); padding-bottom: 8px; margin-bottom: 15px; flex-shrink: 0; }
.shdr h2 { font-size: 22pt; font-weight: 900; }
.shdr .info { text-align: right; font-size: 10pt; color: var(--text-light); }

.pg { display: grid; gap: 3mm 5mm; align-content: stretch; flex: 1; }
.pg.p4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.pg.p6 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; }
.pg.p8 { grid-template-columns: 1fr 1fr; grid-template-rows: repeat(4, 1fr); }
.pg.p10 { grid-template-columns: 1fr 1fr; grid-template-rows: repeat(5, 1fr); }
.pg.p12 { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: repeat(4, 1fr); }
.pg.p15 { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: repeat(5, 1fr); }
.pg.p20 { grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: repeat(5, 1fr); }

.pc { border: 1px solid #f1f5f9; padding: 3mm 3mm; display: flex; flex-direction: column; position: relative; justify-content: center; align-items: center; overflow: hidden; }
.pn { position: absolute; top: 2mm; left: 2mm; font-size: 10pt; font-weight: 800; color: #cbd5e1; }

/* Math Grid */
.math-grid { display: inline-grid; font-family: 'Nanum Gothic Coding', monospace; font-size: 17pt; margin: 0 auto; gap: 0; }
.pg.p4 .math-grid { font-size: 18pt; }
.pg.p8 .math-grid { font-size: 15pt; }
.pg.p10 .math-grid { font-size: 13pt; }
.pg.p12 .math-grid, .pg.p15 .math-grid { font-size: 13pt; }
.pg.p20 .math-grid { font-size: 11pt; --cell-w: 1.1em; --cell-h: 1.3em; }
.cell { width: var(--cell-w); height: var(--cell-h); display: flex; align-items: center; justify-content: center; position: relative; }

/* Vertical Guide Lines */
.cell::after { content: ''; position: absolute; right: 0; top: 0; bottom: 0; border-right: 1px dotted var(--guide); }
.no-guide::after, .last-col::after, .ans-sheet .cell::after { display: none; }

/* Lines */
.h-line { height: 0; border-top: 2px solid var(--text); grid-column: 1 / -1; margin: 4px 0; }
.h-line.double { border-top: 4px double var(--text); }

/* Colors for Answer Sheets */
.ans-sheet .work-cell { color: var(--color-work); }
.ans-sheet .ans-cell { color: var(--color-ans); font-weight: 800; }
.ans-sheet .work-line { border-top: 1.5px solid var(--color-work); height: 0; margin: 2px 0; grid-column: span var(--span); }
.ans-sheet .pc { min-height: auto; padding-bottom: 3mm; }

/* Division Bracket */
.div-v-line { border-right: 2px solid var(--text); } 
.div-h-line { border-top: 2px solid var(--text); }  

@media print {
  @page { size: A4; margin: 0; }
  body { background: white; padding: 0; margin: 0; }
  .controls, .nav-header { display: none !important; }
  .sheet { 
    margin: 0 !important; 
    box-shadow: none !important; 
    width: 210mm; 
    height: 296mm; /* A4(297mm)보다 1mm 작게 설정하여 빈 페이지 방지 */
    padding: 10mm 15mm; 
    break-after: page; 
    page-break-after: always;
    overflow: hidden;
    position: relative;
  }
  .sheet:last-child { break-after: auto; page-break-after: auto; }
  .pc { overflow: hidden; }
  .modal-overlay { display: none !important; }
}

/* Dashboard Specific Styles */
.nav-header {
  background: white; padding: 15px 20px; display: flex; justify-content: space-between; align-items: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1); margin-bottom: 20px;
}
.nav-header .logo { font-size: 1.2rem; font-weight: 900; color: var(--primary); text-decoration: none; }
.nav-header .nav-links { display: flex; gap: 20px; }
.nav-header .nav-links a { text-decoration: none; color: var(--text-light); font-weight: 600; font-size: 0.9rem; }
.nav-header .nav-links a:hover { color: var(--primary); }

.dashboard { max-width: 1000px; margin: 40px auto; padding: 0 20px; }
.dashboard h2 { margin-bottom: 24px; font-size: 1.5rem; font-weight: 800; }
.grid-menu { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 24px; }
.card {
  background: white; border-radius: 16px; padding: 24px; text-decoration: none; color: inherit;
  box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); transition: all 0.2s; border: 2px solid transparent;
  display: flex; flex-direction: column; gap: 12px;
}
.card:hover { transform: translateY(-4px); border-color: var(--primary); box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
.card .icon { font-size: 2.5rem; }
.card .title { font-size: 1.2rem; font-weight: 700; color: var(--text); }
.card .desc { font-size: 0.9rem; color: var(--text-light); line-height: 1.5; }
.card .badge {
  align-self: flex-start; padding: 4px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 700;
  background: #f1f5f9; color: #64748b;
}
.card .badge.g6 { background: #fee2e2; color: #ef4444; }
