/* ===== CSS RESET & VARIABLES ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #4f46e5;
  --primary-dark: #3730a3;
  --primary-light: #818cf8;
  --success: #22c55e;
  --danger: #ef4444;
  --danger-dark: #b91c1c;
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-surface: #16213e;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #2d2d4e;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overscroll-behavior: none;
}

/* ===== TYPOGRAPHY ===== */
h1, h2, h3 { font-weight: 700; line-height: 1.2; }

/* ===== LAYOUT ===== */
#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ===== HEADER ===== */
.app-header {
  background: var(--bg-card);
  padding: 16px 20px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header .back-btn {
  background: none;
  border: none;
  color: var(--primary-light);
  font-size: 24px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  display: flex;
  align-items: center;
}

.app-header h1 {
  font-size: 20px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  gap: 8px;
}

/* ===== LOGIN SCREEN ===== */
#login-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 24px;
}

.login-logo {
  font-size: 64px;
  line-height: 1;
}

.login-title {
  font-size: 28px;
  text-align: center;
}

.login-subtitle {
  color: var(--text-muted);
  text-align: center;
  font-size: 14px;
}

.login-form {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-error {
  color: var(--danger);
  font-size: 14px;
  text-align: center;
  min-height: 20px;
}

/* ===== SCREENS ===== */
.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.screen-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

/* ===== FORM ELEMENTS ===== */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 14px;
  color: var(--text-muted);
  font-weight: 500;
}

input[type="text"],
input[type="password"],
textarea {
  width: 100%;
  background: var(--bg-surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  padding: 14px 16px;
  outline: none;
  transition: border-color var(--transition);
  -webkit-appearance: none;
}

input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus {
  border-color: var(--primary);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-muted);
}

textarea {
  resize: none;
  min-height: 80px;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font);
  font-size: 16px;
  font-weight: 600;
  padding: 14px 20px;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1.5px solid var(--border);
}
.btn-ghost:hover { border-color: var(--text-muted); color: var(--text); }

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: var(--danger-dark); }

.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
  min-width: 44px;
  min-height: 44px;
}
.btn-icon:hover { color: var(--text); background: var(--border); }

.btn-full { width: 100%; }

/* ===== FAB ===== */
.fab {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  right: 24px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 28px;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(79,70,229,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
}
.fab:active { transform: scale(0.92); }
.fab:hover { background: var(--primary-dark); box-shadow: 0 6px 24px rgba(79,70,229,0.6); }

/* ===== CHECKLIST CARDS ===== */
.checklists-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: calc(80px + var(--safe-bottom));
}

.checklist-card {
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  cursor: pointer;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
  position: relative;
  overflow: hidden;
}

.checklist-card:hover,
.checklist-card:focus {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.checklist-card:active { transform: scale(0.98); }

.card-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
}

.card-title {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
  word-break: break-word;
}

.card-desc {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 12px;
  line-height: 1.5;
}

.card-actions {
  display: flex;
  gap: 4px;
}

/* ===== PROGRESS BAR ===== */
.progress-wrap {
  margin-top: 4px;
}

.progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.progress-label .count {
  font-weight: 600;
  color: var(--primary-light);
}

.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 99px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary-light));
  border-radius: 99px;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.progress-fill.done {
  background: linear-gradient(90deg, var(--success), #4ade80);
}

/* ===== EMPTY STATE ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 64px 24px;
  text-align: center;
  color: var(--text-muted);
}

.empty-icon {
  font-size: 56px;
  line-height: 1;
  opacity: 0.6;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.empty-text {
  font-size: 14px;
  line-height: 1.6;
}

/* ===== ITEMS LIST ===== */
.items-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: calc(80px + var(--safe-bottom));
}

.item-row {
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  position: relative;
  overflow: hidden;
  transition: all var(--transition);
  touch-action: pan-y;
}

.item-row.done {
  border-color: rgba(34,197,94,0.3);
  background: rgba(34,197,94,0.05);
}

.item-row.swiping-left {
  background: rgba(239,68,68,0.15);
  border-color: rgba(239,68,68,0.4);
}

.item-checkbox {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.item-checkbox:hover { border-color: var(--success); }

.item-row.done .item-checkbox {
  background: var(--success);
  border-color: var(--success);
}

.item-checkbox-check {
  color: #fff;
  font-size: 14px;
  display: none;
}

.item-row.done .item-checkbox-check { display: block; }

.item-text {
  flex: 1;
  font-size: 16px;
  line-height: 1.5;
  word-break: break-word;
  cursor: pointer;
  min-height: 24px;
}

.item-row.done .item-text {
  text-decoration: line-through;
  color: var(--text-muted);
}

.item-edit-input {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 1.5px solid var(--primary);
  border-radius: 0;
  padding: 0 0 2px 0;
  font-size: 16px;
  color: var(--text);
  outline: none;
}

.item-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

/* Swipe delete hint */
.item-delete-hint {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  background: var(--danger);
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;
  font-size: 20px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--transition);
}

.item-row.swiping-left .item-delete-hint { opacity: 1; }

/* ===== CHECKLIST DETAIL HEADER ===== */
.detail-progress {
  background: var(--bg-card);
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

/* ===== ADD ITEM FORM ===== */
.add-item-bar {
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  display: flex;
  gap: 10px;
  align-items: center;
  position: sticky;
  bottom: 0;
}

.add-item-bar input {
  flex: 1;
  padding: 12px 14px;
  font-size: 16px;
}

.add-item-bar .btn {
  padding: 12px 18px;
  flex-shrink: 0;
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
}

#confirm-modal-overlay {
  z-index: 400;
  padding: 0;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal {
  background: var(--bg-card);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px 20px;
  padding-bottom: calc(24px + var(--safe-bottom));
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  animation: slideUp 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideUp {
  from { transform: translateY(100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 99px;
  margin: 0 auto -8px;
}

.modal-title {
  font-size: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
}

.modal-actions .btn { flex: 1; }

/* ===== LOADING ===== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== TOAST ===== */
.toast-container {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: var(--shadow);
  animation: toastIn 0.3s ease forwards;
}

.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.success { border-color: var(--success); color: var(--success); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastOut {
  to { opacity: 0; transform: translateY(8px) scale(0.96); }
}

/* ===== SCROLL BEHAVIOR ===== */
::-webkit-scrollbar { width: 0; background: transparent; }

/* ===== CATEGORIES ===== */
.category-filter {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  overflow-x: auto;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-shrink: 0;
}
.category-filter::-webkit-scrollbar { display: none; }

.cat-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--bg-surface);
  border: 1.5px solid var(--border);
  border-radius: 99px;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  padding: 5px 14px;
  white-space: nowrap;
  cursor: pointer;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
  font-family: var(--font);
}
.cat-chip:hover { border-color: var(--primary-light); color: var(--text); }
.cat-chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.cat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.cat-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
  padding: 3px 10px;
}

/* Categories modal list */
.cat-manage-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.cat-manage-name {
  flex: 1;
  font-size: 15px;
}

/* Color dot picker */
.color-dots {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.color-dot {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: all var(--transition);
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}
.color-dot.selected { border-color: #fff; transform: scale(1.15); }

/* Select */
select {
  width: 100%;
  background: var(--bg-surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  padding: 14px 16px;
  outline: none;
  transition: border-color var(--transition);
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
select:focus { border-color: var(--primary); }

/* ===== HIDDEN UTILITY ===== */
.hidden { display: none !important; }

/* ===== TABLET+ ===== */
@media (min-width: 600px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .fab {
    bottom: 32px;
    right: calc(50% - 240px + 24px);
  }

  .modal-overlay {
    align-items: center;
  }

  .modal {
    border-radius: var(--radius);
    max-width: 420px;
  }

  @keyframes slideUp {
    from { transform: translateY(16px) scale(0.98); opacity: 0; }
    to { transform: none; opacity: 1; }
  }
}
