:root {
  --bg: #fff8f1;
  --surface: #ffffff;
  --surface-2: #fff1e4;
  --ink: #3a2e2a;
  --ink-soft: #7a6a62;
  --line: #f0dfd0;
  --primary: #ff7a45;
  --primary-ink: #ffffff;
  --primary-soft: #ffe6d9;
  --accent: #ffd166;
  --ok: #2bb673;
  --danger: #e5484d;
  --focus: #3b82f6;
  --shadow: 0 6px 20px rgba(160, 90, 40, .10);
  --radius: 22px;
  --radius-sm: 14px;
  --today: #fff3c4;
  --overlay: rgba(30, 20, 15, .55);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1614; --surface: #26201d; --surface-2: #2f2724; --ink: #f6ece6; --ink-soft: #b8a79e;
    --line: #3b322e; --primary: #ff8a5b; --primary-soft: #43302a; --accent: #f0c05a; --today: #3a3220;
    --shadow: 0 6px 20px rgba(0, 0, 0, .35); --overlay: rgba(0, 0, 0, .75); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1614; --surface: #26201d; --surface-2: #2f2724; --ink: #f6ece6; --ink-soft: #b8a79e;
  --line: #3b322e; --primary: #ff8a5b; --primary-soft: #43302a; --accent: #f0c05a; --today: #3a3220;
  --shadow: 0 6px 20px rgba(0, 0, 0, .35); --overlay: rgba(0, 0, 0, .75); color-scheme: dark;
}

* { box-sizing: border-box; }
html { font-size: 17px; -webkit-text-size-adjust: 100%; }
@media (min-width: 1600px) { html { font-size: 21px; } }  /* TV */
@media (min-width: 2400px) { html { font-size: 28px; } }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.45; min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); }
img { max-width: 100%; }
:focus { outline: none; }
:focus-visible, .tv :focus { outline: 4px solid var(--focus); outline-offset: 3px; }
.hidden { display: none !important; }
.boot { display: grid; place-items: center; min-height: 100vh; font-size: 3rem; animation: spin 2.5s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 10px 20px; border-radius: 999px; border: 2px solid transparent;
  background: var(--surface-2); color: var(--ink); font-weight: 800; text-decoration: none;
  transition: transform .12s, background .12s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-sm { min-height: 38px; padding: 6px 14px; font-size: .9rem; }
.btn .ic { margin-right: 8px; }
.icon-btn {
  width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--surface-2);
  font-size: 1.3rem; display: inline-grid; place-items: center; flex: none;
}
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-weight: 800; margin-bottom: 6px; font-size: .95rem; }
.input {
  width: 100%; min-height: 50px; padding: 12px 16px; border-radius: var(--radius-sm);
  border: 2px solid var(--line); background: var(--surface); font-size: 1rem;
}
.input:focus { border-color: var(--primary); }
textarea.input { min-height: 110px; resize: vertical; }
.row { display: flex; flex-wrap: wrap; align-items: center; }
.row > * + * { margin-left: 10px; }
.spacer { flex: 1; }
.muted { color: var(--ink-soft); }

/* ---------- Login ---------- */
.login {
  min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(circle at 20% 10%, var(--primary-soft), transparent 45%),
              radial-gradient(circle at 85% 90%, rgba(255, 209, 102, .35), transparent 45%), var(--bg);
}
.login-card {
  width: 100%; max-width: 420px; background: var(--surface); border-radius: 28px; padding: 36px 28px;
  box-shadow: var(--shadow); text-align: center;
}
.login-card .logo { width: 88px; height: 88px; margin: 0 auto 12px; display: block; }
.login-card h1 { margin: 0 0 4px; font-size: 1.9rem; font-weight: 900; }
.login-card p { margin: 0 0 24px; color: var(--ink-soft); }
.login-card .input { text-align: center; font-size: 1.25rem; letter-spacing: .1em; }
.login-card .btn { width: 100%; margin-top: 12px; font-size: 1.1rem; min-height: 56px; }
.err { color: var(--danger); font-weight: 700; min-height: 1.4em; margin-top: 10px; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--bg);
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
.topbar-inner { max-width: 1500px; margin: 0 auto; display: flex; align-items: center; flex-wrap: wrap; }
.brand { display: flex; align-items: center; font-weight: 900; font-size: 1.25rem; margin-right: 16px; text-decoration: none; color: var(--ink); }
.brand img { width: 40px; height: 40px; margin-right: 10px; }
.nav { display: flex; background: var(--surface-2); border-radius: 999px; padding: 4px; }
.nav a {
  padding: 10px 18px; border-radius: 999px; text-decoration: none; color: var(--ink);
  font-weight: 800; white-space: nowrap;
}
.nav a.active { background: var(--surface); box-shadow: var(--shadow); color: var(--primary); }
.top-actions { margin-left: auto; display: flex; align-items: center; }
.top-actions > * + * { margin-left: 8px; }
.who-btn { border: 0; background: var(--surface-2); border-radius: 999px; padding: 8px 14px; font-weight: 800; min-height: 48px; }
main { max-width: 1500px; margin: 0 auto; padding: 20px 16px 120px; }
.fab {
  position: fixed; right: 20px; bottom: max(20px, env(safe-area-inset-bottom)); z-index: 30;
  min-height: 64px; padding: 0 26px; font-size: 1.15rem; box-shadow: 0 10px 30px rgba(255, 122, 69, .45);
}
@media (max-width: 720px) {
  .brand span { display: none; }
  .nav { order: 3; width: 100%; margin-top: 10px; justify-content: space-between; }
  .nav a { flex: 1; text-align: center; padding: 10px 4px; font-size: .9rem; }
  .nav a .emo { display: none; }
  .card h2 { flex-wrap: wrap; }
}

/* ---------- Cards / sections ---------- */
.hero { margin: 6px 0 22px; }
.hero h1 { margin: 0; font-size: clamp(1.8rem, 4vw, 2.6rem); font-weight: 900; }
.hero p { margin: 4px 0 0; color: var(--ink-soft); font-size: 1.1rem; font-weight: 700; }
.grid-2 { display: grid; grid-template-columns: 1fr; grid-gap: 18px; }
@media (min-width: 900px) { .grid-2 { grid-template-columns: 1.1fr 1fr; } }
.card { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 14px; font-size: 1.3rem; font-weight: 900; display: flex; align-items: center; }
.card h2 .spacer { flex: 1; }
.section-title { display: flex; align-items: center; margin: 28px 0 14px; }
.section-title h2 { margin: 0; font-size: 1.35rem; font-weight: 900; }
.empty { text-align: center; color: var(--ink-soft); padding: 26px 10px; font-weight: 700; }
.empty .big { font-size: 2.4rem; display: block; margin-bottom: 6px; }

/* Subjects */
.subjects { list-style: none; margin: 0; padding: 0; }
.subject {
  display: flex; align-items: center; padding: 10px 12px; border-radius: var(--radius-sm);
  margin-bottom: 8px; background: var(--sc, var(--surface-2));
}
.session { font-weight: 900; font-size: .85rem; color: var(--ink-soft); margin: 12px 0 6px 4px; text-transform: uppercase; letter-spacing: .04em; }
.session:first-child { margin-top: 0; }
.subject .emo { font-size: 1.5rem; width: 2.1rem; text-align: center; flex: none; }
.subject .name { font-weight: 800; }
.subject .time { font-size: .85rem; color: var(--ink-soft); font-weight: 700; margin-left: auto; padding-left: 8px; white-space: nowrap; }
.subject .sub-note { display: block; font-size: .85rem; color: var(--ink-soft); font-weight: 600; }
.day-note { background: var(--today); border-radius: var(--radius-sm); padding: 10px 14px; font-weight: 700; margin-top: 10px; }

/* Events */
.events { list-style: none; margin: 0; padding: 0; }
.event {
  display: flex; align-items: flex-start; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--surface-2); margin-bottom: 8px; border-left: 6px solid var(--ec, var(--primary));
  width: 100%; text-align: left; border-top: 0; border-right: 0; border-bottom: 0;
}
.event .emo { font-size: 1.3rem; margin-right: 10px; }
.event .t { font-weight: 800; display: block; }
.event .d { font-size: .85rem; color: var(--ink-soft); font-weight: 700; }
.event[data-type="nhac-nho"] { --ec: #ff9f1c; }
.event[data-type="kiem-tra"] { --ec: #e5484d; }
.event[data-type="su-kien"] { --ec: #8b5cf6; }
.event[data-type="nghi"] { --ec: #2bb673; }

/* ---------- Week (thời khóa biểu) ---------- */
.week-nav { display: flex; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.week-nav > * { margin: 4px 8px 4px 0; }
.week-nav .label { font-weight: 900; font-size: 1.15rem; margin: 0 8px; }
.week { display: grid; grid-template-columns: 1fr; grid-gap: 14px; }
@media (min-width: 760px) { .week { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1100px) { .week { grid-template-columns: repeat(5, 1fr); } .week.has-weekend { grid-template-columns: repeat(5, 1fr) .8fr; } }
.day { background: var(--surface); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); border: 3px solid transparent; }
.day.today { border-color: var(--primary); background: linear-gradient(var(--today), var(--surface) 120px); }
.day-head { display: flex; align-items: baseline; margin-bottom: 10px; }
.day-head b { font-size: 1.15rem; font-weight: 900; }
.day-head span { margin-left: 8px; color: var(--ink-soft); font-weight: 700; }
.day-head .pill { margin-left: auto; background: var(--primary); color: var(--primary-ink); border-radius: 999px; padding: 2px 10px; font-size: .8rem; font-weight: 800; }
.day .subject { padding: 8px 10px; }
.day .subject .emo { font-size: 1.25rem; width: 1.8rem; }
.day .events { margin-top: 10px; }

/* ---------- Library ---------- */
.chips { display: flex; overflow-x: auto; padding: 4px 2px 10px; margin: 0 -2px 8px; -webkit-overflow-scrolling: touch; }
.chip {
  flex: none; border: 2px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 8px 16px; font-weight: 800; margin-right: 8px; min-height: 44px; white-space: nowrap;
}
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 12px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(3, 1fr); grid-gap: 16px; } }
@media (min-width: 1000px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
@media (min-width: 1400px) { .tiles { grid-template-columns: repeat(5, 1fr); } }
.tile {
  position: relative; display: block; width: 100%; padding: 0; border: 0; text-align: left;
  background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
  transition: transform .15s;
}
.tile:hover, .tile:focus { transform: translateY(-3px); }
.tile .thumb {
  aspect-ratio: 4 / 3; background: var(--surface-2) center / cover no-repeat;
  display: grid; place-items: center; font-size: 2.6rem; position: relative;
}
@supports not (aspect-ratio: 1) { .tile .thumb { height: 0; padding-top: 75%; } .tile .thumb > * { position: absolute; top: 40%; left: 0; right: 0; text-align: center; } }
.tile .play {
  width: 56px; height: 56px; border-radius: 50%; background: rgba(0, 0, 0, .55); color: #fff;
  display: grid; place-items: center; font-size: 1.4rem; padding-left: 4px;
}
.tile .badge {
  position: absolute; top: 10px; left: 10px; background: var(--surface); color: var(--ink);
  border-radius: 999px; padding: 2px 10px; font-size: .78rem; font-weight: 800; box-shadow: var(--shadow);
}
.tile .meta { padding: 10px 12px 12px; }
.tile .title {
  font-weight: 800; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 2.9em;
}
.tile .sub { font-size: .8rem; color: var(--ink-soft); font-weight: 700; margin-top: 2px; }
.tile.note .thumb { background: #fff4b8; color: #5c4a00; font-size: .95rem; font-weight: 700; padding: 46px 14px 14px; place-items: start; text-align: left; overflow: hidden; }
:root[data-theme="dark"] .tile.note .thumb { background: #4a4020; color: #fbeaa0; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tile.note .thumb { background: #4a4020; color: #fbeaa0; } }
.load-more { display: flex; justify-content: center; margin-top: 20px; }

/* ---------- Modal ---------- */
.modal-wrap {
  position: fixed; inset: 0; z-index: 100; background: var(--overlay);
  display: flex; align-items: flex-end; justify-content: center; padding: 0;
  top: 0; right: 0; bottom: 0; left: 0;
}
@media (min-width: 700px) { .modal-wrap { align-items: center; padding: 24px; } }
.modal {
  background: var(--surface); width: 100%; max-width: 620px; max-height: 92vh; overflow: auto;
  border-radius: 26px 26px 0 0; padding: 22px 20px calc(22px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}
@media (min-width: 700px) { .modal { border-radius: 26px; } }
.modal.wide { max-width: 900px; }
.modal-head { display: flex; align-items: center; margin-bottom: 16px; }
.modal-head h3 { margin: 0; font-size: 1.35rem; font-weight: 900; flex: 1; }
.modal-foot { display: flex; flex-wrap: wrap; justify-content: flex-end; margin-top: 18px; }
.modal-foot > * { margin: 4px 0 4px 10px; }
.modal-foot .left { margin-right: auto; margin-left: 0; }
.seg { display: flex; background: var(--surface-2); border-radius: 999px; padding: 4px; margin-bottom: 16px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 999px; padding: 10px 8px; font-weight: 800; min-height: 44px; }
.seg button.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }
.drop {
  display: block; border: 3px dashed var(--line); border-radius: var(--radius); padding: 28px 16px;
  text-align: center; font-weight: 800; color: var(--ink-soft); cursor: pointer;
}
.drop .big { font-size: 2.4rem; display: block; }
.drop input { display: none; }
.files { list-style: none; padding: 0; margin: 12px 0 0; }
.files li { display: flex; align-items: center; padding: 6px 0; font-weight: 700; font-size: .92rem; }
.files .fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { width: 90px; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-left: 10px; }
.bar i { display: block; height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.who-grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 12px; }
.who-grid button {
  border: 3px solid var(--line); background: var(--surface); border-radius: var(--radius); padding: 18px 8px;
  font-weight: 900; font-size: 1.05rem;
}
.who-grid button .big { font-size: 2.6rem; display: block; }
.who-grid button.active { border-color: var(--primary); background: var(--primary-soft); }

/* Timetable editor */
.tt-ver { display: grid; grid-template-columns: 1fr; grid-gap: 0 12px; }
@media (min-width: 600px) { .tt-ver { grid-template-columns: 1.6fr 1fr; } }
.tt-info { margin: -4px 0 16px; color: var(--ink-soft); font-weight: 700; }
.tt-info b { color: var(--ink); }
.tt-row { display: grid; grid-template-columns: 110px 1fr 44px; grid-gap: 8px; margin-bottom: 8px; align-items: center; }
.tt-row .input { min-height: 46px; padding: 8px 12px; }
.tt-row .note-in { grid-column: 1 / 3; }
.tt-row .del { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--surface-2); color: var(--danger); font-size: 1.1rem; }
.tt-item { border-bottom: 1px dashed var(--line); padding-bottom: 8px; margin-bottom: 10px; }
@media (min-width: 700px) {
  .tt-row { grid-template-columns: 110px 1.3fr 1fr 44px; }
  .tt-row .note-in { grid-column: auto; }
  .tt-item { border: 0; padding: 0; margin: 0; }
}

/* ---------- Viewer (ảnh/video toàn màn hình) ---------- */
.viewer {
  position: fixed; inset: 0; top: 0; right: 0; bottom: 0; left: 0; z-index: 200; background: #000;
  display: flex; flex-direction: column; color: #fff;
}
.viewer-stage { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; position: relative; }
.viewer-stage img, .viewer-stage video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-stage iframe { width: 100%; height: 100%; border: 0; }
.viewer-stage .note-view { max-width: 800px; padding: 30px; font-size: 1.3rem; white-space: pre-wrap; overflow: auto; max-height: 100%; }
.viewer-bar { display: flex; align-items: center; padding: 10px 14px; padding-bottom: max(10px, env(safe-area-inset-bottom)); background: rgba(0, 0, 0, .7); }
.viewer-bar .info { flex: 1; min-width: 0; margin: 0 12px; }
.viewer-bar .info b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-bar .info small { color: #bbb; font-weight: 700; }
.viewer .icon-btn { background: rgba(255, 255, 255, .15); color: #fff; }
.viewer .icon-btn + .icon-btn { margin-left: 8px; }
.viewer .nav-arrow {
  position: absolute; top: 50%; transform: translateY(-50%); width: 60px; height: 60px; border-radius: 50%;
  border: 0; background: rgba(255, 255, 255, .15); color: #fff; font-size: 1.8rem;
}
.viewer .nav-arrow.prev { left: 14px; }
.viewer .nav-arrow.next { right: 14px; }

/* ---------- PWA ---------- */
.steps { padding-left: 1.4em; margin: 0 0 12px; font-size: 1.05rem; line-height: 1.7; }
.steps li { margin-bottom: 6px; }
.kbd { display: inline-block; min-width: 1.8em; text-align: center; border: 2px solid var(--line); border-radius: 8px; padding: 0 6px; font-weight: 900; }
@media (max-width: 720px) { .install-btn span { display: none; } }
@media (display-mode: standalone) { .install-btn { display: none !important; } }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 100px; transform: translateX(-50%) translateY(20px); z-index: 300;
  background: var(--ink); color: var(--bg); padding: 12px 20px; border-radius: 999px; font-weight: 800;
  opacity: 0; pointer-events: none; transition: all .25s; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
