:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e3e7ef;
  --primary: #4f8cff;
  --primary-dark: #3a6fe0;
  --danger: #e0554f;
  --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 14px; }

/* ---------- Auth gate ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-wrap[hidden] { display: none; }
.auth-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 10px 30px rgba(20, 30, 60, 0.08);
}
.brand {
  font-size: 22px;
  font-weight: 700;
  color: var(--primary-dark);
}
.tagline { color: var(--muted); font-size: 14px; margin: 8px 0 20px; }
.auth-tabs { display: flex; gap: 4px; background: var(--bg); border-radius: 8px; padding: 4px; margin-bottom: 16px; }
.tab-btn { flex: 1; border: none; background: transparent; padding: 8px; border-radius: 6px; font-weight: 600; color: var(--muted); }
.tab-btn.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.auth-form { display: flex; flex-direction: column; gap: 4px; }
.auth-form[hidden] { display: none; }
.auth-form label { font-size: 13px; color: var(--muted); margin-top: 8px; }
.auth-form input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; margin-top: 4px;
}
.password-field { position: relative; margin-top: 4px; }
.password-field input { width: 100%; margin-top: 0; padding-right: 56px; }
.show-password-btn {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  border: none; background: transparent; color: var(--primary-dark); font-size: 12px; font-weight: 600; padding: 4px 6px;
}
.auth-form button[type="submit"] {
  margin-top: 18px; padding: 11px; border: none; border-radius: 8px;
  background: var(--primary); color: #fff; font-weight: 600; font-size: 14px;
}
.auth-form button[type="submit"]:hover { background: var(--primary-dark); }
.msg { min-height: 18px; font-size: 13px; margin-top: 10px; }
.msg.error { color: var(--danger); }
.msg.ok { color: #1a9f6c; }

/* ---------- App shell ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--card); border-bottom: 1px solid var(--border);
}
.topbar-right { display: flex; align-items: center; gap: 14px; font-size: 14px; color: var(--muted); }

.layout {
  display: flex; flex-direction: column; gap: 16px;
  padding: 16px 20px; max-width: 760px; margin: 0 auto;
  min-width: 0;
}

.quick-add-panel { background: var(--card); border-radius: var(--radius); padding: 16px 18px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.quick-add { display: flex; gap: 6px; }
.quick-add input { flex: 1; min-width: 0; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; }
.quick-add button { padding: 10px 14px; border: none; border-radius: 8px; background: var(--primary); color: #fff; font-weight: 600; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.4; margin: 0; }
.hint code { background: var(--bg); padding: 1px 4px; border-radius: 4px; }

.secondary-btn {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--card); font-weight: 600; font-size: 13px;
  align-self: flex-start;
}
.secondary-btn:hover { background: var(--bg); }

.category-filters { display: flex; flex-direction: column; gap: 6px; }
.category-filter-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.category-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.link-btn { border: none; background: transparent; color: var(--primary-dark); font-size: 13px; font-weight: 600; padding: 4px 0; text-align: left; }

/* ---------- Task list panel ---------- */
.task-list-panel { background: var(--card); border-radius: var(--radius); padding: 16px 18px; min-width: 0; }
.task-list-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
#range-select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--card); font-weight: 600; font-size: 13px; }
.category-dropdown { position: relative; }
.category-filter-panel {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 15;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; min-width: 200px; box-shadow: 0 8px 24px rgba(20, 30, 60, 0.14);
}
.category-filter-panel[hidden] { display: none; }
#task-list-title { font-size: 16px; margin: 0 0 10px; }
.task-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.task-list li {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; cursor: pointer; font-size: 14px;
}
.task-list li:hover { background: var(--bg); }
.task-list .task-checkbox { width: 18px; height: 18px; flex-shrink: 0; margin: 0; }
.task-list .task-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.task-list .task-title.done { text-decoration: line-through; color: var(--muted); }
.task-list .task-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.task-list-empty { color: var(--muted); font-size: 14px; padding: 6px 0; }

/* ---------- Calendar ---------- */
.calendar-panel { background: var(--card); border-radius: var(--radius); padding: 18px; min-width: 0; }
.calendar-header { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.calendar-header h2 { margin: 0; font-size: 18px; }
.view-note { margin-left: auto; font-size: 12px; color: var(--muted); }

.ghost-btn { border: 1px solid var(--border); background: var(--card); border-radius: 8px; padding: 6px 10px; }
.ghost-btn:hover { background: var(--bg); }
.danger-btn { border: 1px solid var(--danger); color: var(--danger); background: #fff; border-radius: 8px; padding: 8px 12px; font-weight: 600; }
.danger-btn:hover { background: #fdeceb; }

/* minmax(0, 1fr), not a bare 1fr — otherwise long unbroken item titles force
   the grid tracks wider than the viewport instead of respecting the ellipsis. */
.calendar-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; min-width: 0; }
.cal-weekday { background: var(--bg); padding: 8px 2px; text-align: center; font-size: 12px; font-weight: 700; color: var(--muted); }
.cal-cell { background: var(--card); min-height: 96px; min-width: 0; padding: 6px; display: flex; flex-direction: column; gap: 4px; cursor: pointer; }
.cal-cell:hover { background: var(--bg); }
.cal-cell.out-of-month { background: #fafbfd; color: var(--muted); }
.cal-cell.today .cal-date { background: var(--primary); color: #fff; border-radius: 50%; }
.cal-date { font-size: 12px; width: 22px; height: 22px; display: flex; align-items: center; justify-content: center; }
.cal-item { min-width: 0; font-size: 11px; padding: 2px 5px; border-radius: 5px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-item.done { opacity: 0.5; text-decoration: line-through; }
.cal-more { font-size: 11px; color: var(--muted); }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 24, 36, 0.4);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 20;
}
.modal-backdrop[hidden] { display: none; }
.modal { background: var(--card); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 440px; max-height: 90vh; overflow-y: auto; }
.modal h3 { margin: 0 0 14px; }
.modal label { display: block; font-size: 13px; color: var(--muted); margin-top: 10px; }
.modal input[type="text"], .modal input[type="date"], .modal input[type="datetime-local"],
.modal select, .modal textarea {
  width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; margin-top: 4px;
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); margin-top: 12px; }
.checkbox-row input { width: auto; margin: 0; }
.modal-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 20px; }
.modal-actions-right { display: flex; gap: 8px; }
.modal-actions button[type="submit"] { border: none; background: var(--primary); color: #fff; border-radius: 8px; padding: 9px 16px; font-weight: 600; }

.category-list { list-style: none; margin: 0 0 14px; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; }
.category-list li { display: flex; align-items: center; gap: 8px; }
.category-list input[type="text"] { flex: 1; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.category-list input[type="color"] { width: 32px; height: 32px; border: none; padding: 0; background: none; }
.category-list button { border: none; background: transparent; color: var(--danger); font-size: 13px; }
.add-category-form { display: flex; gap: 6px; }
.add-category-form input[type="text"] { flex: 1; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; }
.add-category-form input[type="color"] { width: 36px; height: 36px; border: none; padding: 0; background: none; }
.add-category-form button { border: none; background: var(--primary); color: #fff; border-radius: 8px; padding: 0 14px; font-weight: 600; }

/* ---------- Small phones ---------- */
@media (max-width: 480px) {
  .layout { padding: 10px 10px; gap: 12px; }
  .topbar { padding: 12px 14px; }
  .calendar-panel, .quick-add-panel, .task-list-panel { padding: 12px; }
  .calendar-header h2 { font-size: 15px; }
  .view-note { display: none; }
  .cal-cell { min-height: 64px; padding: 3px; gap: 2px; }
  .cal-date { width: 18px; height: 18px; font-size: 11px; }
  .cal-item { font-size: 9px; padding: 1px 3px; }
  .cal-weekday { font-size: 10px; padding: 6px 1px; }
  .modal { padding: 16px; }
  .field-row { grid-template-columns: 1fr; }
}
