:root {
  --bg: #0f1117;
  --bg-soft: #171a23;
  --bg-card: #1d212c;
  --line: #2a2f3d;
  --text: #e8eaf0;
  --text-dim: #9aa3b5;
  --accent: #6d7cff;
  --accent-soft: rgba(109, 124, 255, .16);
  --done: #3ddc97;
  --warn: #ffb454;
  --danger: #ff5f6d;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, .45);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --bg-soft: #ffffff;
    --bg-card: #ffffff;
    --line: #e2e6ef;
    --text: #1a1e29;
    --text-dim: #69707e;
    --accent: #4f5fe8;
    --accent-soft: rgba(79, 95, 232, .12);
    --done: #12a56b;
    --warn: #d98316;
    --danger: #e0424e;
    --shadow: 0 8px 26px rgba(24, 33, 61, .13);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overscroll-behavior: none;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background .15s;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn:active { transform: scale(.94); }

.title-btn {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  padding: 6px 8px;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
}
.title-btn:hover { background: var(--accent-soft); }
.caret { font-size: 10px; color: var(--text-dim); }

.today-btn {
  width: auto;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
}

/* ---------- 主体 ---------- */
.body {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  position: relative;
  /* 关闭状态的侧滑面板用 translateX(105%) 移到右侧外面，
     若不裁剪会把文档 scrollWidth 撑大（桌面端实测 1440 → 1839）。
     只裁横向、纵向保持 visible，避免短视口时日历被截掉。 */
  overflow-x: clip;
  overflow-y: visible;
}

.cal-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 8px 10px 0;
  /* 桌面端面板打开时给日历让出空间，避免遮住最后一列 */
  transition: padding-right .22s cubic-bezier(.4, 0, .2, 1);
}
@media (min-width: 721px) {
  .body.panel-open .cal-wrap { padding-right: calc(min(380px, 100%) + 10px); }
}

.weekhead {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  padding-bottom: 6px;
  flex: 0 0 auto;
}
.weekhead div {
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  padding: 2px 0;
}
.weekhead div.wk { color: var(--warn); opacity: .75; }

.grid {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: minmax(0, 1fr);
  gap: 4px;
  min-height: 0;
  padding-bottom: 8px;
}

.cell {
  position: relative;
  background: var(--bg-card);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 4px 5px;
  min-height: 0;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background .15s, border-color .15s, transform .1s;
}
.cell:hover { border-color: var(--line); }
.cell:active { transform: scale(.985); }

.cell.dim { opacity: .38; }
.cell.today { border-color: var(--accent); background: var(--accent-soft); }
.cell.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }

.cell .num {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
  flex: 0 0 auto;
}
.cell.dim .num { color: var(--text-dim); }
.cell.today .num { color: var(--accent); }
.cell.wk .num { color: var(--warn); }

.chips {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  min-height: 0;
  flex: 1 1 auto;
}

.chip {
  font-size: 11px;
  line-height: 1.35;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 3px;
}
.chip::before {
  content: "";
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
}
.chip.pri { background: rgba(255, 180, 84, .18); }
.chip.pri::before { background: var(--warn); }
.chip.done { opacity: .5; text-decoration: line-through; }
.chip.done::before { background: var(--done); }

.more {
  font-size: 10px;
  color: var(--text-dim);
  padding-left: 2px;
  flex: 0 0 auto;
}

/* ---------- 当日面板 ---------- */
.day-panel {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: min(380px, 100%);
  background: var(--bg-soft);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transform: translateX(105%);
  transition: transform .22s cubic-bezier(.4, 0, .2, 1);
  z-index: 20;
}
.day-panel.open { transform: translateX(0); }

.day-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 14px 14px 8px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.day-head h2 { margin: 0; font-size: 17px; font-weight: 650; }
.day-sub { margin: 2px 0 0; font-size: 12px; color: var(--text-dim); }
.close-btn { font-size: 16px; }

.list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.li {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  transition: background .15s;
}
.li:hover { background: var(--accent-soft); }

.check {
  width: 20px; height: 20px;
  border-radius: 6px;
  border: 2px solid var(--line);
  background: transparent;
  cursor: pointer;
  flex: 0 0 auto;
  margin-top: 1px;
  display: grid;
  place-items: center;
  color: transparent;
  font-size: 12px;
  transition: all .15s;
}
.check:hover { border-color: var(--accent); }
.check.on {
  background: var(--done);
  border-color: var(--done);
  color: #08130d;
  font-weight: 700;
}

.li-body { flex: 1 1 auto; min-width: 0; }
.li-title {
  font-size: 14px;
  word-break: break-word;
  cursor: pointer;
}
.li.done .li-title { color: var(--text-dim); text-decoration: line-through; }
.li-note {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
  white-space: pre-wrap;
  word-break: break-word;
}
.li-flag { color: var(--warn); font-size: 12px; flex: 0 0 auto; margin-top: 2px; }

.add-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 12px);
  border-top: 1px solid var(--line);
  background: var(--bg-soft);
  flex: 0 0 auto;
}
.add-form input[type=text] {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.add-form input[type=text]:focus { border-color: var(--accent); }
.add-btn {
  width: 42px; height: 42px;
  flex: 0 0 auto;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.add-btn:active { transform: scale(.94); }

/* ---------- 底栏 ---------- */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px calc(env(safe-area-inset-bottom) + 8px);
  border-top: 1px solid var(--line);
  background: var(--bg-soft);
  font-size: 12px;
  color: var(--text-dim);
  flex: 0 0 auto;
}
.link-btn {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}
.link-btn:hover { background: var(--accent-soft); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 76px;
  transform: translate(-50%, 20px);
  background: var(--bg-card);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 100;
  max-width: 82vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 200;
  backdrop-filter: blur(3px);
}
.modal[hidden] { display: none; }

.modal-card {
  width: min(440px, 100%);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  box-shadow: var(--shadow);
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card h3 { margin: 0 0 16px; font-size: 17px; font-weight: 650; }

.field { display: block; margin-bottom: 13px; }
.field > span {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 5px;
  font-weight: 600;
}
.field input, .field textarea {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: vertical;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  margin-bottom: 16px;
  cursor: pointer;
  color: var(--text);
}
.check-row input { width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; }

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}
.spacer { flex: 1 1 auto; }

.btn {
  border: 1px solid var(--line);
  background: var(--bg-card);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: transparent; background: rgba(255, 95, 109, .1); }
.btn.ghost { background: transparent; }

.empty {
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  padding: 34px 16px;
}

/* ---------- 手机 ---------- */
@media (max-width: 720px) {
  body { font-size: 14px; }

  .cal-wrap { padding: 6px 6px 0; }

  .weekhead { gap: 2px; }
  .weekhead div { font-size: 11px; }

  .grid { gap: 2px; padding-bottom: 6px; }

  .cell {
    border-radius: 8px;
    padding: 3px 3px;
    align-items: center;
    justify-content: flex-start;
    gap: 0;
  }
  .cell .num { font-size: 13px; }
  .cell .chips { display: none; }

  /* 手机用圆点表示当天有任务 */
  .dots {
    display: flex;
    gap: 2px;
    margin-top: 2px;
    flex-wrap: wrap;
    justify-content: center;
  }
  .dot { width: 4px; height: 4px; border-radius: 50%; background: var(--accent); }
  .dot.pri { background: var(--warn); }
  .dot.done { background: var(--done); opacity: .55; }

  .day-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--line);
    top: auto;
    height: 68%;
    border-radius: 18px 18px 0 0;
    transform: translateY(105%);
  }
  .day-panel.open { transform: translateY(0); }

  .today-btn { padding: 0 10px; font-size: 12px; }
  .title-btn { font-size: 16px; }

  .toast { bottom: 92px; }
}

@media (min-width: 721px) {
  .dots { display: none; }
}

/* 不显示滚动条但保留滚动 */
.list::-webkit-scrollbar { width: 6px; }
.list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }