/* ── Variables ─────────────────────────────────────────── */
:root {
  --bg:           #FBF6EE;
  --bg2:          #F3EDE0;
  --bg3:          #EAE0CE;
  --primary:      #2B5BAE;
  --primary-dk:   #1E4080;
  --primary-lt:   #4A78CC;
  --primary-pale: #E8EFFC;
  --warm:         #C97B5B;
  --warm-lt:      #E8A882;
  --warm-pale:    #FAEEE6;
  --text:         #1A1A2E;
  --text-mid:     #5A5A6E;
  --text-muted:   #9898AA;
  --done-text:    #A0A0B0;
  --border:       #DDD5C8;
  --border-lt:    #EDE8DF;
  --white:        #FFFFFF;
  --red:          #D94040;
  --shadow-sm:    0 1px 3px rgba(0,0,0,.08);
  --shadow-md:    0 4px 16px rgba(0,0,0,.12);
  --radius:       8px;
  --sidebar-w:    260px;
  --topbar-h:     56px;
  --addbar-h:     68px;
  --transition:   0.18s ease;
}

/* ── Reset ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button { cursor: pointer; border: none; background: none; font: inherit; }
input  { font: inherit; }

ul { list-style: none; }

.hidden { display: none !important; }

/* ── Auth view ─────────────────────────────────────────── */
.auth-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: linear-gradient(145deg, #EEE6D8 0%, #DCE8F8 100%);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--white);
  border-radius: 16px;
  padding: 40px 36px;
  box-shadow: var(--shadow-md);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
}

.auth-brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
}

.auth-card h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 24px;
  color: var(--text);
}

.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-mid);
  margin-bottom: 6px;
  letter-spacing: .02em;
  text-transform: uppercase;
}

.field input {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.field input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-pale);
}

.auth-error {
  color: var(--red);
  font-size: 14px;
  margin-bottom: 12px;
  padding: 10px 12px;
  background: #FFF0F0;
  border-radius: var(--radius);
  border: 1px solid #F8CCCC;
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: var(--white);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  margin-top: 8px;
  transition: background var(--transition), transform var(--transition);
}

.btn-primary:hover  { background: var(--primary-dk); }
.btn-primary:active { transform: scale(0.98); }

.auth-switch {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: var(--text-mid);
}

/* ── App view layout ───────────────────────────────────── */
.app-view {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

/* ── Sidebar ───────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  height: 100%;
  overflow: hidden;
  transition: transform var(--transition);
  z-index: 200;
}

.sidebar-top {
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border-lt);
}

.sidebar-brand {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -.01em;
}

.context-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.ctx-item {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  cursor: pointer;
  border-radius: 0;
  transition: background var(--transition);
  gap: 8px;
}

.ctx-item:hover { background: var(--bg3); }

.ctx-item.active {
  background: var(--primary-pale);
  color: var(--primary);
  font-weight: 600;
}

.ctx-item.active .ctx-name { color: var(--primary); }

.ctx-name {
  flex: 1;
  font-size: 15px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ctx-count {
  font-size: 12px;
  font-weight: 600;
  color: var(--white);
  background: var(--warm);
  border-radius: 10px;
  padding: 1px 7px;
  min-width: 22px;
  text-align: center;
}

.ctx-item.active .ctx-count { background: var(--primary); }

.sidebar-bottom {
  padding: 16px 20px;
  border-top: 1px solid var(--border-lt);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.add-ctx-btn {
  padding: 9px 14px;
  background: var(--primary-pale);
  color: var(--primary);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  text-align: left;
  transition: background var(--transition);
}
.add-ctx-btn:hover { background: #D6E4F8; }

.sidebar-user {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.user-email {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-btn {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}
.logout-btn:hover { color: var(--red); }

/* ── Main wrap ─────────────────────────────────────────── */
.main-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  position: relative;
}

/* ── Top bar ───────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border-lt);
  flex-shrink: 0;
  position: relative;
  z-index: 100;
}

.context-title {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  color: var(--text-mid);
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--bg2); color: var(--text); }

/* Hide menu button on desktop */
@media (min-width: 768px) {
  #menu-btn { display: none; }
}

/* ── Overflow menu ─────────────────────────────────────── */
.overflow-menu {
  position: absolute;
  top: calc(var(--topbar-h) + 4px);
  right: 12px;
  z-index: 500;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  min-width: 230px;
  padding: 6px 0;
  animation: fadeUp .15s ease;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.overflow-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 16px;
  font-size: 14px;
  color: var(--text);
  transition: background var(--transition);
}
.overflow-item:hover { background: var(--bg2); }
.overflow-item.danger { color: var(--red); }
.overflow-item.danger:hover { background: #FFF0F0; }

.overflow-divider { height: 1px; background: var(--border-lt); margin: 4px 0; }

.overflow-label {
  padding: 6px 16px 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

/* ── Content area ──────────────────────────────────────── */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0 calc(var(--addbar-h) + 16px);
}

.empty-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 60%;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.6;
}

.empty-hero-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--bg2);
  color: var(--border);
  font-size: 26px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Task list & items ─────────────────────────────────── */
.task-list {
  padding: 0 16px;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-radius: var(--radius);
  min-height: 48px;
  transition: background var(--transition), opacity var(--transition);
  position: relative;
}

.task-item:hover { background: var(--bg2); }
.task-item:hover .task-actions { opacity: 1; }

/* Custom checkbox */
.task-cb {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color var(--transition), background var(--transition);
  cursor: pointer;
}

.task-cb:hover { border-color: var(--primary-lt); }

.task-cb.checked {
  background: var(--primary);
  border-color: var(--primary);
}

.task-cb.checked::after {
  content: '';
  display: block;
  width: 5px;
  height: 9px;
  border: 2px solid var(--white);
  border-top: none;
  border-left: none;
  transform: rotate(45deg) translate(-1px, -1px);
}

/* Warm checkbox for done tasks in done section */
.done-list .task-cb {
  background: var(--warm);
  border-color: var(--warm);
}
.done-list .task-cb:hover {
  background: var(--warm-lt);
  border-color: var(--warm-lt);
}

.task-text {
  flex: 1;
  font-size: 15px;
  color: var(--text);
  line-height: 1.4;
  word-break: break-word;
  cursor: text;
}

.task-text.done-text {
  color: var(--done-text);
  text-decoration: line-through;
  text-decoration-color: var(--warm-lt);
  cursor: default;
}

.task-edit-input {
  flex: 1;
  font-size: 15px;
  color: var(--text);
  border: none;
  border-bottom: 1.5px solid var(--primary);
  background: transparent;
  outline: none;
  padding: 0;
  line-height: 1.4;
}

.task-ts {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.task-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity var(--transition);
  flex-shrink: 0;
}

/* Always visible on touch devices */
@media (hover: none) {
  .task-actions { opacity: 1; }
}

.task-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}

.task-btn:hover { background: var(--bg3); color: var(--text); }
.task-btn.del:hover { background: #FFEEEE; color: var(--red); }
.task-btn.arch:hover { background: var(--primary-pale); color: var(--primary); }

.empty-state {
  padding: 16px 12px;
  font-size: 14px;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Done section ──────────────────────────────────────── */
.done-section {
  margin-top: 8px;
  border-top: 1px solid var(--border-lt);
  padding-top: 4px;
}

.done-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px 6px 12px;
}

.done-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: .03em;
  padding: 4px 6px;
  border-radius: 6px;
  transition: background var(--transition), color var(--transition);
}
.done-toggle:hover { background: var(--bg2); color: var(--text-mid); }

.caret {
  transition: transform var(--transition);
}
.caret path { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.caret.closed { transform: rotate(-90deg); }

.archive-done-btn {
  font-size: 13px;
  color: var(--warm);
  font-weight: 500;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background var(--transition);
}
.archive-done-btn:hover { background: var(--warm-pale); }

.done-footer {
  padding: 8px 16px 4px;
}

.view-archive-btn {
  font-size: 13px;
  color: var(--primary);
  font-weight: 500;
  padding: 4px 0;
}
.view-archive-btn:hover { text-decoration: underline; }

.show-more-btn {
  font-size: 13px;
  color: var(--primary);
  padding: 8px 12px;
  width: 100%;
  text-align: center;
  border-radius: var(--radius);
}
.show-more-btn:hover { background: var(--primary-pale); }

/* ── Add bar ───────────────────────────────────────────── */
.add-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border-lt);
  z-index: 100;
}

.add-form {
  display: flex;
  gap: 10px;
  align-items: center;
}

.add-input {
  flex: 1;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.add-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-pale);
}

.add-submit {
  padding: 11px 20px;
  background: var(--primary);
  color: var(--white);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  flex-shrink: 0;
  transition: background var(--transition), transform var(--transition);
}
.add-submit:hover  { background: var(--primary-dk); }
.add-submit:active { transform: scale(0.97); }

/* ── Archive view (full overlay) ───────────────────────── */
.archive-view {
  position: absolute;
  inset: 0;
  z-index: 300;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  animation: slideIn .2s ease;
}

@keyframes slideIn {
  from { transform: translateX(100%); }
  to   { transform: translateX(0); }
}

.archive-topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  border-bottom: 1px solid var(--border-lt);
  flex-shrink: 0;
}

.archive-topbar h1 {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
}

.archive-content {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0 32px;
}

.archive-ctx-group {
  margin-bottom: 4px;
}

.archive-ctx-name {
  padding: 12px 20px 6px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
}

.archive-empty {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1.6;
}

/* ── Modal ─────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.4);
  animation: fadeIn .15s ease;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  position: relative;
  background: var(--white);
  border-radius: 14px;
  padding: 28px 28px 24px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-md);
  animation: scaleIn .15s ease;
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(.95); }
  to   { opacity: 1; transform: scale(1); }
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
}

.modal-input {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  font-size: 15px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
  margin-bottom: 4px;
}

.modal-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-pale);
}

.modal-error {
  font-size: 13px;
  color: var(--red);
  margin-bottom: 16px;
  min-height: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.modal-cancel {
  padding: 9px 18px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  color: var(--text-mid);
  background: var(--bg2);
}
.modal-cancel:hover { background: var(--bg3); }

.modal-confirm {
  padding: 9px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  color: var(--white);
  background: var(--primary);
}
.modal-confirm:hover { background: var(--primary-dk); }

.modal-confirm.danger { background: var(--red); }
.modal-confirm.danger:hover { background: #B83030; }

/* ── Toasts ────────────────────────────────────────────── */
.toasts {
  position: fixed;
  bottom: calc(var(--addbar-h) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--text);
  color: var(--white);
  border-radius: 10px;
  font-size: 14px;
  box-shadow: var(--shadow-md);
  pointer-events: all;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
  white-space: nowrap;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

.toast-undo {
  color: var(--warm-lt);
  font-weight: 700;
  font-size: 14px;
  text-decoration: underline;
}
.toast-undo:hover { color: var(--warm); }

/* ── Drawer overlay (mobile) ───────────────────────────── */
.drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 190;
  transition: opacity var(--transition);
}

/* ── Mobile layout ─────────────────────────────────────── */
@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-md);
  }

  .drawer-overlay.open {
    display: block;
  }

  .context-title {
    text-align: center;
  }

  .task-actions { opacity: 1; }

  .overflow-menu {
    right: 8px;
    left: 8px;
    min-width: unset;
  }
}

/* ── Desktop ───────────────────────────────────────────── */
@media (min-width: 768px) {
  .content {
    padding-left: 8px;
    padding-right: 8px;
  }

  .task-list {
    padding: 0 24px;
  }

  .done-header {
    padding-left: 20px;
    padding-right: 20px;
  }
}

/* ── Scrollbar styling ─────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Task animation helpers ────────────────────────────── */
.task-item.fading {
  opacity: 0;
  transform: translateX(16px);
  pointer-events: none;
}
