:root {
  --bg: #f4f5f7; --panel: #fff; --text: #1f2329; --muted: #646a73; --line: #e4e6eb;
  --brand: #2563eb; --brand-soft: #e8efff; --ok: #16a34a; --ok-soft: #e7f6ec; --warn: #d97706; --warn-soft: #fdf3e2;
  --bad: #dc2626; --bad-soft: #fde8e8; --bubble: #e8efff; --radius: 10px;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317; --panel: #1b1e24; --text: #e6e8eb; --muted: #9aa0a8; --line: #2c3038;
    --brand: #4c82f7; --brand-soft: #1d2a45; --ok-soft: #12301d; --warn-soft: #3a2b10; --bad-soft: #3d1717; --bubble: #1d2a45;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 18px; } h3 { font-size: 15px; }
.muted { color: var(--muted); } .small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- Khung ---------- */
.shell { display: grid; grid-template-rows: auto 1fr auto; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 8px; padding: calc(8px + var(--safe-t)) 16px 8px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 52px; }
.topbar .title { font-weight: 600; font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iconbtn { border: 0; background: none; padding: 6px 8px; border-radius: 8px; cursor: pointer; font-size: 19px; position: relative; line-height: 1; }
.iconbtn:hover { background: var(--bg); }
.dot { position: absolute; top: 0; right: 0; background: var(--bad); color: #fff; font-size: 10px; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 4px; font-weight: 700; }
main { overflow: auto; -webkit-overflow-scrolling: touch; min-height: 0; }
.page { padding: 16px; max-width: 1100px; margin: 0 auto; }
.nav { display: flex; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
.nav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 0 6px; color: var(--muted); font-size: 11px; position: relative; }
.nav a .ic { font-size: 21px; line-height: 1; }
.nav a.on { color: var(--brand); font-weight: 600; }
.nav a .dot { top: 2px; right: calc(50% - 22px); }
.nav a.desk { display: none; }
@media (min-width: 900px) { .nav a.desk { display: flex; } #back { display: none; } }

@media (min-width: 900px) {
  .shell { grid-template-columns: 210px 1fr; grid-template-rows: auto 1fr; }
  .topbar { grid-column: 2; }
  .nav { grid-row: 1 / 3; grid-column: 1; flex-direction: column; border-top: 0; border-right: 1px solid var(--line); padding: 12px 8px; gap: 2px; }
  .nav::before { content: attr(data-company); font-weight: 700; font-size: 16px; padding: 6px 12px 14px; }
  .nav a { flex: none; flex-direction: row; gap: 10px; padding: 9px 12px; font-size: 14px; border-radius: 8px; }
  .nav a.on { background: var(--brand-soft); }
  .nav a .ic { font-size: 18px; }
  .nav a .dot { position: static; margin-left: auto; }
  main { grid-column: 2; }
}

/* ---------- Thành phần chung ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.btn { border: 1px solid var(--line); background: var(--panel); padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.bad { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ghost { border-color: transparent; background: none; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
label.f { display: block; margin-bottom: 12px; }
label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=time], input[type=month], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
textarea { resize: vertical; min-height: 70px; }
.tabs { display: flex; gap: 4px; margin-bottom: 14px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs a { padding: 8px 12px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.on { color: var(--brand); border-color: var(--brand); font-weight: 600; }
.list > * { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); color: var(--text); background: var(--panel); }
.list > *:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.list > *:last-child { border-bottom: 0; border-radius: 0 0 var(--radius) var(--radius); }
.list { border: 1px solid var(--line); border-radius: var(--radius); }
.list > a:hover { background: var(--bg); }
.avatar { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; flex: none; font-size: 15px; }
.avatar.sm { width: 30px; height: 30px; font-size: 12px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.approved { background: var(--ok-soft); color: var(--ok); }
.badge.rejected { background: var(--bad-soft); color: var(--bad); }
.badge.cancelled { background: var(--bg); color: var(--muted); }
.count { background: var(--bad); color: #fff; border-radius: 99px; font-size: 12px; padding: 0 7px; font-weight: 700; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.ell { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.t { width: 100%; border-collapse: collapse; background: var(--panel); }
table.t th, table.t td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
table.t th { background: var(--bg); font-weight: 600; font-size: 13px; white-space: nowrap; }
.scrollx { overflow-x: auto; border-radius: var(--radius); }

#toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%); z-index: 99; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
#toast div { background: #1f2329; color: #fff; padding: 9px 16px; border-radius: 8px; box-shadow: 0 4px 16px #0003; max-width: 90vw; pointer-events: auto; cursor: pointer; }
#toast div.err { background: var(--bad); }

.modal-bg { position: fixed; inset: 0; background: #0006; z-index: 50; display: grid; place-items: end center; }
.modal { background: var(--panel); width: 100%; max-width: 520px; max-height: 88dvh; overflow: auto; border-radius: 14px 14px 0 0; padding: 18px 16px calc(18px + var(--safe-b)); }
@media (min-width: 600px) { .modal-bg { place-items: center; } .modal { border-radius: 14px; } }
.modal h2 { margin-bottom: 14px; }
.checklist { max-height: 260px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.checklist label { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }

/* ---------- Đăng nhập ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; padding: 26px 22px; }
.login img { width: 56px; height: 56px; border-radius: 14px; display: block; margin: 0 auto 12px; }

/* ---------- Chat ---------- */
.chat { display: grid; grid-template-columns: 1fr; height: 100%; }
.chat .chlist { overflow: auto; background: var(--panel); }
.chat .room { display: none; }
.chat.inroom .chlist { display: none; }
.chat.inroom .room { display: flex; }
@media (min-width: 900px) {
  .chat { grid-template-columns: 300px 1fr; }
  .chat .chlist { display: block !important; border-right: 1px solid var(--line); }
  .chat .room { display: flex; }
}
.chlist .item { display: flex; gap: 10px; padding: 10px 14px; align-items: center; color: var(--text); border-bottom: 1px solid var(--line); }
.chlist .item.on { background: var(--brand-soft); }
.chlist .item b { font-weight: 600; }
.chlist .tools { display: flex; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.room { flex-direction: column; min-height: 0; height: 100%; }
.room .head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.msgs { flex: 1; overflow: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.msg { display: flex; gap: 8px; max-width: 85%; align-items: flex-end; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg .b { background: var(--panel); border: 1px solid var(--line); padding: 7px 11px; border-radius: 14px; white-space: pre-wrap; word-break: break-word; }
.msg.me .b { background: var(--bubble); border-color: transparent; }
.msg .who { font-size: 12px; color: var(--muted); margin: 8px 0 2px 46px; }
.msg .b img { max-width: 240px; max-height: 300px; border-radius: 8px; display: block; }
.msg .t { font-size: 11px; color: var(--muted); flex: none; }
.mention { color: var(--brand); font-weight: 600; }
.daysep { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0; }
.composer { display: flex; gap: 6px; padding: 8px 10px calc(8px); background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { min-height: 40px; max-height: 140px; resize: none; border-radius: 20px; padding: 9px 14px; }

/* ---------- Chấm công ---------- */
.clock { font-size: 46px; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.netstate { text-align: center; padding: 8px; border-radius: 8px; margin: 10px 0 14px; font-weight: 500; }
.netstate.ok { background: var(--ok-soft); color: var(--ok); }
.netstate.bad { background: var(--bad-soft); color: var(--bad); }
.punch { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.punch .btn { padding: 16px; font-size: 17px; border-radius: 12px; }
.punch .v { text-align: center; font-size: 13px; color: var(--muted); margin-top: 4px; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal div { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 4px; min-height: 52px; font-size: 12px; }
.cal div.off { opacity: .45; }
.cal div b { display: block; font-size: 13px; }
.cal .c-X { border-color: var(--ok); } .cal .c-M, .cal .c-S, .cal .c-MS { border-color: var(--warn); background: var(--warn-soft); }
.cal .c-V { border-color: var(--bad); background: var(--bad-soft); } .cal .c-P { background: var(--brand-soft); }
.cal .h { background: none; border: 0; min-height: 0; text-align: center; color: var(--muted); font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; }
.stats div { background: var(--bg); border-radius: 8px; padding: 8px 10px; }
.stats b { display: block; font-size: 20px; }

/* ---------- Bảng ---------- */
.grid td { padding: 0; min-width: 120px; }
.grid td .cell { padding: 6px 8px; min-height: 32px; cursor: text; white-space: pre-wrap; word-break: break-word; }
.grid td input, .grid td select { border: 0; border-radius: 0; outline: 2px solid var(--brand); padding: 6px 8px; }
.grid th { cursor: pointer; }
.grid td.rn { min-width: 40px; width: 40px; text-align: center; color: var(--muted); font-size: 12px; padding: 6px 2px; }
.tag { display: inline-block; background: var(--brand-soft); color: var(--brand); padding: 0 8px; border-radius: 99px; font-size: 13px; }

/* ---------- Docs ---------- */
.toolbar { display: flex; gap: 2px; flex-wrap: wrap; padding: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; position: sticky; top: 0; z-index: 2; }
.toolbar button { border: 0; background: none; padding: 5px 9px; border-radius: 6px; cursor: pointer; min-width: 32px; }
.toolbar button:hover { background: var(--bg); }
.doc-title { font-size: 24px; font-weight: 700; border: 0 !important; padding: 12px 0 !important; background: none !important; }
.editor { min-height: 60vh; outline: none; padding: 8px 2px 80px; font-size: 16px; line-height: 1.6; }
.editor img { max-width: 100%; border-radius: 6px; }
.editor:empty::before { content: 'Bắt đầu viết...'; color: var(--muted); }
.banner { background: var(--warn-soft); color: var(--warn); padding: 8px 12px; border-radius: 8px; margin: 8px 0; }

/* ---------- Thông báo ---------- */
.notifs { position: absolute; right: 8px; top: calc(52px + var(--safe-t)); width: min(380px, calc(100vw - 16px)); max-height: 70dvh; overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px #0003; z-index: 40; }
.notifs a { display: block; padding: 10px 14px; border-bottom: 1px solid var(--line); color: var(--text); }
.notifs a.unread { background: var(--brand-soft); }
