:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #1b2430;
  --muted: #5d6b7c;
  --line: #dde3ec;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --warn-bg: #fff4d6;
  --warn-ink: #8a5a00;
  --danger: #c0392b;
  --ok: #1e8e5a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 14px rgba(16, 24, 40, .06);
  --t1: #1f6feb; --t2: #e4572e; --t3: #17a398; --t4: #f0a202; --t5: #8e44ad;
  --t6: #2e86de; --t7: #d35400; --t8: #27ae60; --t9: #c2185b; --t10: #607d8b;
  --t11: #00897b; --t12: #6d4c41;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Pretendard", "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  font-size: 15px; line-height: 1.5;
}
h1, h2, h3, h4 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.2rem; margin-bottom: .5rem; }
h3 { font-size: 1.02rem; margin-bottom: .4rem; }
p { margin: .3rem 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.hint { color: var(--muted); font-size: .86rem; font-weight: 400; }
.desc { color: var(--muted); margin-bottom: .8rem; }
.row { display: flex; gap: .5rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
[hidden] { display: none !important; }

/* ---------- 상단 ---------- */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .8rem 1.2rem; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: .9rem; flex: 1 1 360px; min-width: 0; }
.brand .logo-img { height: 46px; width: auto; flex: none; }
.brand .brand-text { border-left: 1px solid var(--line); padding-left: .9rem; min-width: 0; }
.brand .sub { margin: 0; color: var(--muted); font-size: .82rem; }
.topbar-actions { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.inline { display: inline-flex; align-items: center; gap: .3rem; font-size: .9rem; }
.switch { display: inline-flex; align-items: center; gap: .4rem; cursor: pointer; font-size: .9rem; padding: .3rem .6rem; border: 1px solid var(--line); border-radius: 999px; }
.switch input { accent-color: var(--accent); }

.steps { display: flex; gap: .4rem; padding: .6rem 1.2rem; flex-wrap: wrap; }
.steps a { text-decoration: none; color: var(--muted); background: var(--card); border: 1px solid var(--line); padding: .3rem .7rem; border-radius: 999px; font-size: .88rem; }
.steps a b { display: inline-block; width: 1.3em; height: 1.3em; line-height: 1.3em; text-align: center; border-radius: 50%; background: var(--accent); color: #fff; font-size: .8em; margin-right: .3em; }

main { max-width: 1240px; margin: 0 auto; padding: .4rem 1.2rem 3rem; display: grid; grid-template-columns: minmax(0, 1fr); gap: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; box-shadow: var(--shadow); min-width: 0; }
/* 긴 파일명·헤더 문자열이 화면 폭을 넘기지 않도록 */
.card, .topbar, .steps { overflow-wrap: anywhere; }
.import-grid > *, .settings-grid > *, .cons-grid > *, .export-grid > *, .teams-grid > *, .charts > * { min-width: 0; }
.sec-head { display: flex; justify-content: space-between; align-items: center; gap: .8rem; flex-wrap: wrap; margin-bottom: .4rem; }
footer { max-width: 1240px; margin: 0 auto; padding: 0 1.2rem 2rem; color: var(--muted); font-size: .8rem; }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex; align-items: center; gap: .3rem; cursor: pointer;
  padding: .45rem .9rem; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink);
  font-size: .9rem; white-space: nowrap; transition: background .12s, box-shadow .12s;
}
.btn:hover { background: #f3f6fb; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #185ed0; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.big { padding: .7rem 1.4rem; font-size: 1rem; }
.btn.small { padding: .2rem .5rem; font-size: .8rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.actions { display: flex; gap: .6rem; margin-top: 1rem; flex-wrap: wrap; }

/* ---------- 불러오기 ---------- */
.import-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 1rem; }
.import-panel { border: 1px solid var(--line); border-radius: 10px; padding: .8rem; background: #fbfcfe; }
.dropzone { border: 2px dashed #b9c6d8; border-radius: 10px; padding: .9rem; text-align: center; background: #fff; transition: border-color .12s, background .12s; }
.dropzone.over { border-color: var(--accent); background: #eef4ff; }
.dropzone p { margin: 0 0 .5rem; color: var(--muted); }
.dropzone .row { justify-content: center; }
.paste-box { margin-top: .6rem; }
.paste-box textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: .5rem; font-family: ui-monospace, Consolas, monospace; font-size: .82rem; }
.paste-box .row { margin-top: .4rem; }
.import-status { margin-top: .6rem; font-size: .88rem; }
.import-status .ok { color: var(--ok); }
.mapping { margin-top: .6rem; }
.mapping .map-box { border: 1px solid var(--line); border-radius: 10px; background: #fff; padding: .7rem; }
.mapping table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.mapping th, .mapping td { text-align: left; padding: .25rem .3rem; border-bottom: 1px solid #eef1f5; }
.mapping tr.group td { background: #f2f5fa; font-weight: 600; color: var(--muted); }
.mapping select { width: 100%; max-width: 300px; padding: .2rem; border: 1px solid var(--line); border-radius: 6px; }
.mapping .preview { overflow: auto; max-height: 160px; margin-top: .5rem; border: 1px solid var(--line); border-radius: 8px; }
.mapping .preview table { font-size: .76rem; white-space: nowrap; }
.mapping .preview th { background: #f2f5fa; position: sticky; top: 0; }
.mapping .warn { color: var(--warn-ink); background: var(--warn-bg); padding: .3rem .5rem; border-radius: 6px; margin: .3rem 0; font-size: .84rem; }
.help { margin-top: .8rem; font-size: .88rem; color: var(--muted); }
.help summary { cursor: pointer; color: var(--accent); }

/* ---------- 명단 ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
table.roster { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.roster th, table.roster td { padding: .3rem .45rem; border-bottom: 1px solid #eef1f5; text-align: center; white-space: nowrap; }
table.roster th { background: #f2f5fa; position: sticky; top: 0; z-index: 1; }
table.roster tr:hover td { background: #f9fbfe; }
table.roster input, table.roster select { width: 100%; min-width: 3.2em; padding: .2rem .3rem; border: 1px solid transparent; border-radius: 6px; background: transparent; text-align: center; }
table.roster input:hover, table.roster select:hover { border-color: var(--line); }
table.roster input:focus, table.roster select:focus { border-color: var(--accent); background: #fff; outline: none; }
table.roster input[type=text] { text-align: left; min-width: 6em; }
table.roster td.num { font-variant-numeric: tabular-nums; }
table.roster td.num > span:first-child { margin-right: .25rem; }
table.roster input::placeholder { color: #b5bfcc; }
table.roster .gcell { display: inline-flex; align-items: center; gap: .25rem; white-space: nowrap; }
table.roster .gcell select { width: auto; min-width: 3.4em; }
table.roster .gcell select.inferred { border-color: #e2c97e; background: #fffaf0; }
.badge { display: inline-block; font-size: .72rem; padding: .05rem .4rem; border-radius: 999px; background: #e9eef6; color: var(--muted); margin: 0 .1rem; }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge.ok { background: #e3f6ec; color: var(--ok); }
.badge.bad { background: #fde8e6; color: var(--danger); }
.summary { color: var(--muted); font-size: .88rem; }
.empty { color: var(--muted); text-align: center; padding: 1.2rem; }

/* ---------- 설정 ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: .7rem 1.2rem; }
.settings-grid label { display: flex; flex-direction: column; gap: .25rem; font-weight: 600; font-size: .9rem; }
.settings-grid label.check { flex-direction: row; align-items: center; gap: .5rem; flex-wrap: wrap; }
.settings-grid input[type=number], .settings-grid select { padding: .35rem .5rem; border: 1px solid var(--line); border-radius: 8px; max-width: 320px; font-weight: 400; }
.settings-grid input[type=range] { accent-color: var(--accent); max-width: 320px; }
.settings-grid input[type=checkbox] { accent-color: var(--accent); width: 1.1em; height: 1.1em; }
.constraints { margin-top: 1rem; border-top: 1px dashed var(--line); padding-top: .8rem; }
.cons-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: .8rem; }
.cons-box { border: 1px solid var(--line); border-radius: 10px; padding: .6rem .7rem; background: #fbfcfe; }
.cons-box h4 { font-size: .92rem; margin-bottom: .4rem; }
.cons-box ul { list-style: none; margin: 0 0 .4rem; padding: 0; font-size: .86rem; }
.cons-box li { display: flex; justify-content: space-between; align-items: center; padding: .15rem 0; border-bottom: 1px solid #eef1f5; }
.cons-box select { padding: .25rem; border: 1px solid var(--line); border-radius: 6px; max-width: 46%; }
.cons-box .row { flex-wrap: wrap; }

.draft-launch { margin-top: 1.2rem; border-top: 1px dashed var(--line); padding-top: .9rem; }
.mode-pick { gap: .6rem; }
.radio { display: inline-flex; flex-direction: column; gap: .1rem; padding: .5rem .8rem; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; min-width: 200px; flex: 1 1 200px; }
.radio:has(input:checked) { border-color: var(--accent); background: #eef4ff; }
.radio input { accent-color: var(--accent); margin-right: .3rem; }
.radio b { font-size: .92rem; }
.radio .hint { font-size: .78rem; }

/* ---------- 결과 ---------- */
.result-summary { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; margin: .3rem 0 .8rem; }
.equity { display: flex; align-items: center; gap: .6rem; padding: .5rem .8rem; border-radius: 10px; background: #f2f5fa; }
.equity .score { font-size: 1.7rem; font-weight: 800; color: var(--accent); }
.equity .label { font-size: .8rem; color: var(--muted); }
.kv { font-size: .86rem; color: var(--muted); }
.kv b { color: var(--ink); }
.log { font-size: .84rem; color: var(--warn-ink); background: var(--warn-bg); border-radius: 8px; padding: .4rem .7rem; margin-bottom: .6rem; }
.teams-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: .8rem; }
.team { border: 1px solid var(--line); border-radius: 12px; background: #fff; overflow: hidden; transition: box-shadow .12s, outline-color .12s; }
.team.over { box-shadow: 0 0 0 3px rgba(31, 111, 235, .25); }
.team-head { padding: .5rem .7rem; color: #fff; display: flex; justify-content: space-between; align-items: baseline; gap: .4rem; }
.team-head h3 { margin: 0; font-size: 1rem; }
.team-head .meta { font-size: .78rem; opacity: .92; white-space: nowrap; }
.team-stats { display: flex; gap: .4rem; flex-wrap: wrap; padding: .4rem .6rem; font-size: .76rem; color: var(--muted); border-bottom: 1px solid #eef1f5; background: #fbfcfe; }
.team-stats span b { color: var(--ink); font-weight: 600; }
.members { list-style: none; margin: 0; padding: .45rem .5rem .55rem; min-height: 2.4rem; display: flex; flex-direction: column; gap: .25rem; }
.member {
  display: flex; align-items: center; gap: .4rem; padding: .3rem .5rem; border-radius: 8px;
  background: #f5f7fb; cursor: grab; user-select: none; font-size: .9rem;
}
.member:active { cursor: grabbing; }
.member.selected { outline: 2px solid var(--accent); background: #eaf1ff; }
.member .no { color: var(--muted); font-size: .8rem; min-width: 1.6em; text-align: right; font-variant-numeric: tabular-nums; }
.member .g { font-size: .7rem; padding: 0 .35rem; border-radius: 999px; background: #e9eef6; color: var(--muted); }
.member .g.m { background: #e3ecff; color: #1f4fa3; }
.member .g.f { background: #ffe4ec; color: #a3244b; }
.member .sc { margin-left: auto; font-size: .74rem; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.member .pin { font-size: .75rem; }
.unassigned { border: 1px dashed #c9a227; background: #fffaf0; border-radius: 10px; padding: .5rem .7rem; margin-bottom: .8rem; }
.unassigned .members { flex-direction: row; flex-wrap: wrap; padding: .3rem 0 0; }

.balance { margin-top: 1rem; overflow: auto; }
.balance table { border-collapse: collapse; width: 100%; font-size: .86rem; }
.balance th, .balance td { padding: .3rem .5rem; border-bottom: 1px solid #eef1f5; text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums; }
.balance th { background: #f2f5fa; }
.balance tr.foot td { background: #fbfcfe; font-weight: 600; }
.balance td.tn { text-align: left; font-weight: 600; }
.balance .dot { display: inline-block; width: .7em; height: .7em; border-radius: 50%; margin-right: .35em; vertical-align: -1px; }

.charts { display: grid; grid-template-columns: 1fr; gap: 1rem; margin-top: 1rem; }
@media (min-width: 900px) { .charts { grid-template-columns: 3fr 2fr; } }
.chart { border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem; background: #fff; }
.chart h4 { font-size: .92rem; margin-bottom: .3rem; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .legend { display: flex; gap: .6rem; flex-wrap: wrap; font-size: .78rem; color: var(--muted); margin-top: .3rem; }
.chart .legend i { display: inline-block; width: .7em; height: .7em; border-radius: 50%; margin-right: .3em; }

/* ---------- 내보내기 ---------- */
.export-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }

/* ---------- 토스트 ---------- */
.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); background: #1b2430; color: #fff; padding: .6rem 1rem; border-radius: 10px; font-size: .9rem; z-index: 50; box-shadow: var(--shadow); max-width: 90vw; }
.toast.error { background: var(--danger); }

/* ---------- 학생용 보기 ---------- */
body.student-view .teacher-only { display: none !important; }
body.student-view .member .sc, body.student-view .team-stats { display: none !important; }
body.student-view .member { cursor: default; }
body.student-view main { padding-top: 1rem; }

/* ---------- 인쇄 ---------- */
@media print {
  body { background: #fff; font-size: 12px; }
  .topbar, .steps, footer, .no-print, .teacher-only, .toast, #charts { display: none !important; }
  main { padding: 0; max-width: none; }
  .card { border: none; box-shadow: none; padding: 0; }
  .teams-grid { grid-template-columns: repeat(3, 1fr); gap: .5rem; }
  .team { break-inside: avoid; }
  .team-head { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .member { background: #f3f3f3; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .member .sc, .team-stats { display: none !important; }
  .balance { margin-top: .6rem; }
}

/* 좁은 화면에서는 상단바가 여러 줄로 접히므로 고정하지 않음 (내용 가림 방지) */
@media (max-width: 1100px) {
  .topbar { position: static; }
}
/* ---------- 휴대폰: 폭 맞춤 + 가독성 ---------- */
@media (max-width: 640px) {
  html { font-size: 17px; }
  body { line-height: 1.55; }
  h2 { font-size: 1.3rem; }
  h3 { font-size: 1.1rem; }
  .topbar { padding: .7rem .9rem; gap: .6rem; }
  .brand { gap: .6rem; }
  .brand .logo-img { height: 40px; }
  .brand .brand-text { padding-left: .6rem; }
  .brand h1 { font-size: 1.15rem; }
  .brand .sub { display: none; }
  .topbar-actions { width: 100%; }
  .topbar-actions .inline input { font-size: 1rem; padding: .35rem .5rem; }
  .switch { font-size: 1rem; padding: .4rem .8rem; }
  .steps { padding: .6rem .9rem; gap: .5rem; }
  .steps a { font-size: .98rem; padding: .45rem .85rem; }
  main { padding: .4rem .9rem 2rem; gap: .9rem; }
  .card { padding: 1rem .9rem; }
  .desc, .hint { font-size: .95rem; }
  .btn { padding: .6rem 1.05rem; font-size: 1rem; border-radius: 10px; }
  .btn.small { padding: .3rem .6rem; font-size: .9rem; }
  .import-grid { grid-template-columns: 1fr; }
  .import-panel { padding: .9rem .8rem; }
  .import-panel h3 .hint { display: block; margin-top: .15rem; font-size: .88rem; }
  .dropzone { padding: 1rem .8rem; }
  .dropzone p { font-size: 1rem; }
  .dropzone .row { flex-wrap: wrap; }
  table.roster { font-size: .95rem; }
  table.roster th, table.roster td { padding: .4rem .5rem; }
  .badge { font-size: .8rem; }
  .settings-grid label { font-size: 1rem; }
  .teams-grid { grid-template-columns: 1fr; }
  .member { font-size: 1rem; padding: .45rem .6rem; }
  .team-head h3 { font-size: 1.1rem; }
  .equity .score { font-size: 2rem; }
  .export-grid { grid-template-columns: 1fr; }
}
