/* ===== Màu & biến ===== */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #15171c;
  --muted: #687080;
  --line: #e3e6eb;
  --line-soft: #eff1f4;
  --accent: #0a66ff;
  --accent-text: #ffffff;
  --danger: #d92d20;
  --now: #e5484d;
  --p1: #e8590c; /* Quan trọng + Gấp: đỏ cam */
  --p2: #2563eb; /* Quan trọng, không gấp: xanh dương */
  --p3: #d4a000; /* Gấp, không quan trọng: vàng */
  --p4: #8b919b; /* Không gấp, không quan trọng: xám */
  --p3-on: #1d1a10;
  --shadow: 0 8px 24px rgba(16, 24, 40, 0.14);
  --radius: 12px;
  --gutter: 48px;
  --peek: calc(92px + env(safe-area-inset-bottom));
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --surface: #1b1c20;
    --text: #eceef2;
    --muted: #9aa1ad;
    --line: #2b2e34;
    --line-soft: #202227;
    --accent: #4c8dff;
    --p1: #ff7a3d;
    --p2: #6b98ff;
    --p3: #f2c744;
    --p4: #9ba1aa;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
  }
}

.prio-1 { --pc: var(--p1); }
.prio-2 { --pc: var(--p2); }
.prio-3 { --pc: var(--p3); }
.prio-4 { --pc: var(--p4); }

/* ===== Nền tảng ===== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2 { margin: 0; }

.btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 0 12px; }
.btn[aria-pressed="true"] {
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  border-color: var(--accent);
  color: var(--accent);
}
.btn:disabled { opacity: 0.5; }

.icon-btn {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-size: 26px;
  line-height: 1;
  color: var(--muted);
}
.icon-btn:active { background: var(--line-soft); }

.input {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* >= 16px để iOS không tự phóng to */
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
input[type="date"].input, input[type="time"].input { -webkit-appearance: none; appearance: none; text-align: left; }
input::-webkit-date-and-time-value { text-align: left; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-label { font-size: 13px; color: var(--muted); font-weight: 500; }
.hint { font-size: 13px; color: var(--muted); }

/* ===== Đăng nhập ===== */
.login {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 28px 24px;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 24px; }
.login-msg { margin: 0; min-height: 1.4em; color: var(--danger); font-size: 14px; }

/* ===== Khung app ===== */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.topbar {
  flex: none;
  padding: calc(env(safe-area-inset-top) + 6px) 10px 6px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.nav-row { display: flex; align-items: center; gap: 4px; }
.nav-spacer { flex: 1; }
.date-wrap { position: relative; min-width: 0; }
.date-label {
  display: block;
  padding: 0 4px;
  font-size: 18px;
  font-weight: 650;
  white-space: nowrap;
}
.date-label .short { display: none; }
.date-picker {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  font-size: 16px;
  cursor: pointer;
}
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 50;
  min-width: 160px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.menu button {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  text-align: left;
}
.menu button:hover { background: var(--line-soft); }

.mit-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  white-space: nowrap;
}
.mit-row::-webkit-scrollbar { display: none; }
.mit-count { flex: none; font-size: 14px; font-weight: 600; color: #c08a00; padding: 0 4px; }
.mit-chip {
  flex: none;
  max-width: 60vw;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mit-chip.done { text-decoration: line-through; color: var(--muted); }
.mit-hint { font-size: 13px; color: var(--muted); padding: 6px 4px; }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 340px 1fr;
}

/* ===== Lịch ===== */
.calendar { position: relative; min-height: 0; min-width: 0; display: flex; flex-direction: column; }
.cal-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.cal-empty {
  position: sticky;
  top: 8px;
  z-index: 7;
  margin: 10px 12px 0 calc(var(--gutter) + 4px);
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px dashed var(--line);
  color: var(--muted);
  font-size: 14px;
}
.cal-inner { position: relative; margin: 12px 0 28px; }
.cal-hours { position: absolute; left: 0; top: 0; bottom: 0; width: var(--gutter); }
.hour-label {
  position: absolute;
  right: 8px;
  transform: translateY(-50%);
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.cal-lanes {
  position: absolute;
  left: var(--gutter);
  right: 10px;
  top: 0;
  bottom: 0;
  background-image: repeating-linear-gradient(
    to bottom,
    var(--line) 0 1px,
    transparent 1px 32px,
    var(--line-soft) 32px 33px,
    transparent 33px 64px
  );
  border-left: 1px solid var(--line);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.block {
  position: absolute;
  z-index: 2;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 4px 8px;
  border-radius: 8px;
  border-left: 4px solid var(--pc);
  background: color-mix(in srgb, var(--pc) 17%, var(--surface));
  color: var(--text);
  font-size: 13px;
  overflow: hidden;
  cursor: grab;
  transition: box-shadow 0.15s, transform 0.15s;
  -webkit-touch-callout: none;
}
.block-title { font-weight: 600; line-height: 1.25; overflow: hidden; text-overflow: ellipsis; }
.block:not(.compact) .block-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.block-meta { font-size: 12px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.block.compact { flex-direction: row; align-items: baseline; gap: 6px; padding-top: 2px; padding-bottom: 2px; }
.block.compact .block-title { white-space: nowrap; flex: 0 1 auto; min-width: 0; }
.block.compact .block-meta { flex: none; }
.block .star { color: #d9a300; }
.block { container-type: inline-size; }
/* Khối hẹp (khi trùng giờ chia cột): ưu tiên hiện tên việc */
@container (max-width: 200px) {
  .block.compact .block-meta { display: none; }
  .block-meta { font-size: 11px; }
}
.block.done { opacity: 0.55; }
.block.done .block-title { text-decoration: line-through; }
.block.lifting { z-index: 6; box-shadow: var(--shadow); transform: scale(1.02); cursor: grabbing; transition: none; }
.resize-handle {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 10px;
  cursor: ns-resize;
}
.resize-handle::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 3px;
  width: 24px;
  height: 3px;
  margin-left: -12px;
  border-radius: 2px;
  background: var(--pc);
  opacity: 0;
}
.block:hover .resize-handle::after, .block.lifting .resize-handle::after { opacity: 0.6; }
@media (pointer: coarse) {
  .resize-handle { height: 16px; }
  .resize-handle::after { opacity: 0.45; }
}

.ghost {
  position: absolute;
  left: 2px;
  right: 2px;
  z-index: 5;
  padding: 4px 8px;
  border: 2px dashed var(--accent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
}
.now-line {
  position: absolute;
  left: -5px;
  right: 0;
  z-index: 4;
  height: 2px;
  background: var(--now);
  pointer-events: none;
}
.now-line::before {
  content: "";
  position: absolute;
  left: -4px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--now);
}

body.dragging, body.dragging * { user-select: none; -webkit-user-select: none; cursor: grabbing; }

/* ===== Inbox ===== */
.inbox {
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--bg);
}
.drawer-handle { display: none; }
.inbox-content {
  height: 100%;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.quick-form { display: flex; gap: 8px; }
.quick-preview { display: flex; flex-wrap: wrap; gap: 4px; min-height: 0; }
.quick-preview:empty { display: none; }
.pill {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--line-soft);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.templates { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 13px;
}
.chip[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.chip.tpl { border-left: 4px solid var(--pc); }
@media (pointer: coarse) { .chip { min-height: 44px; } }

.inbox-head { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.inbox-head h2 { font-size: 15px; }
.count {
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--line);
  font-size: 12px;
  text-align: center;
}
.inbox-empty { margin: 0; color: var(--muted); font-size: 14px; }
.inbox-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.inbox-item {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.inbox-item .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--pc); }
.inbox-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inbox-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.inbox-item.lifting { opacity: 0.4; }

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  max-width: 70vw;
  padding: 10px 14px;
  border-left: 4px solid var(--pc);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

/* ===== Bảng sửa việc ===== */
dialog.sheet {
  width: min(560px, calc(100vw - 32px));
  max-width: 100vw;
  max-height: 90dvh;
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.35); }
.sheet-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: 90dvh;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  overflow-y: auto;
}
.sheet-grip { display: none; }
.title-input { font-size: 18px; font-weight: 600; }
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.seg {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 600;
  color: var(--pc);
}
.seg[aria-pressed="true"] { background: var(--pc); border-color: var(--pc); color: #fff; }
.seg.prio-3[aria-pressed="true"] { color: var(--p3-on); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.input.num { width: 88px; min-height: 36px; }
@media (pointer: coarse) { .input.num { min-height: 44px; } }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.mit-toggle {
  min-height: 44px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: transparent;
  font-weight: 500;
}
.mit-toggle[aria-pressed="true"] {
  border-style: solid;
  border-color: #d9a300;
  background: color-mix(in srgb, #f2c744 18%, var(--surface));
}
.mit-toggle:disabled { opacity: 0.45; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.sheet-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 4px;
}

/* ===== Cài đặt ===== */
.sheet-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-title-row h2 { font-size: 20px; }
.section-title {
  margin: 8px 0 0;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.settings-section, .settings-form { display: flex; flex-direction: column; gap: 10px; }
.btn.wide { width: 100%; }
.notice { margin: 0; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.notice.ok { background: color-mix(in srgb, #1f9d55 14%, var(--surface)); color: #157a40; }
.notice.warn { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); color: var(--danger); }
@media (prefers-color-scheme: dark) { .notice.ok { color: #5fd38d; } }
.hint.error { color: var(--danger); }
.device-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.device {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.device-info { flex: 1; min-width: 0; }
.device-name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-name .me { font-weight: 400; color: var(--accent); }
.install-guide {
  padding: 12px 14px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
  font-size: 15px;
}
.install-guide p { margin: 0 0 6px; }
.install-guide ol { margin: 0; padding-left: 22px; display: flex; flex-direction: column; gap: 4px; }
.install-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  padding: 4px 4px 4px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  font-size: 14px;
}
.install-banner span { flex: 1; }
.install-banner .icon-btn { width: 40px; height: 40px; font-size: 22px; }

/* ===== Thông báo nhỏ ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 300;
  max-width: min(92vw, 480px);
  padding: 10px 16px;
  border-radius: 12px;
  background: #24262b;
  color: #fff;
  font-size: 14px;
  box-shadow: var(--shadow);
  transform: translateX(-50%);
}
.toast[data-kind="error"] { background: var(--danger); }

/* ===== Điện thoại ===== */
@media (max-width: 899px) {
  .layout { display: block; position: relative; }
  .calendar { height: 100%; }
  .cal-scroll { padding-bottom: var(--peek); }
  .date-label { font-size: 17px; }
  .date-label .long { display: none; }
  .date-label .short { display: inline; }

  .inbox {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    height: min(78dvh, 640px);
    border: 0;
    border-radius: 18px 18px 0 0;
    background: var(--surface);
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.12);
    transform: translateY(calc(100% - var(--peek)));
    transition: transform 0.25s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .inbox[data-open="true"] { transform: translateY(0); }
  .drawer-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 28px;
    border: 0;
    background: transparent;
    touch-action: none;
  }
  .drawer-handle .grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); }
  .inbox-content { height: calc(100% - 28px); padding-top: 4px; }

  .toast { bottom: calc(var(--peek) + 12px); }

  dialog.sheet {
    width: 100vw;
    margin: auto 0 0;
    border-radius: 18px 18px 0 0;
  }
  .sheet-grip {
    display: block;
    width: 40px;
    height: 5px;
    margin: -6px auto 0;
    border-radius: 3px;
    background: var(--line);
  }
}

@media (max-width: 380px) {
  .btn.small { padding: 0 9px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
