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

:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --border: #dee2e6;
  --text: #212529;
  --text-muted: #6c757d;
  --primary: #0d6efd;
  --primary-hover: #0b5ed7;
  --danger: #dc3545;
  --danger-hover: #bb2d3b;
  --success: #198754;
  --warning: #ffc107;
  --overdue: #dc3545;
  --due-today: #fd7e14;
  --sidebar-width: 220px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

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

button {
  cursor: pointer;
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 0.875rem;
  font-family: inherit;
  transition: background 0.15s;
}

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

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

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

button.btn-sm {
  padding: 3px 8px;
  font-size: 0.8rem;
}

input[type="text"],
input[type="number"],
input[type="date"],
textarea,
select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
}
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

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

/* Layout */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 1.1rem;
}

.sidebar-nav {
  list-style: none;
  padding: 8px 0;
  flex: 1;
}

.sidebar-nav li a {
  display: block;
  padding: 8px 16px;
  color: var(--text);
  font-size: 0.9rem;
  border-radius: 0;
  transition: background 0.1s;
}
.sidebar-nav li a:hover {
  background: var(--bg);
  text-decoration: none;
}
.sidebar-nav li a.active {
  background: #e7f1ff;
  color: var(--primary);
  font-weight: 500;
}

.sidebar-section-label {
  padding: 12px 16px 4px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.sidebar-footer .user-name {
  display: block;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Main content */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 24px;
  max-width: 800px;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.page-title {
  font-size: 1.4rem;
  font-weight: 600;
}

/* Todo list */
.todo-list {
  list-style: none;
}

.todo-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.todo-item:hover {
  border-color: #adb5bd;
}

.todo-check {
  flex-shrink: 0;
  margin-top: 2px;
}

.todo-check button {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: transparent;
  font-size: 12px;
  transition: border-color 0.15s, background 0.15s;
}
.todo-check button:hover {
  border-color: var(--success);
  color: var(--success);
}

.todo-body {
  flex: 1;
  min-width: 0;
}

.todo-title {
  font-weight: 500;
  margin-bottom: 2px;
}

.todo-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-top: 4px;
}

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 0.75rem;
  font-weight: 500;
}

.badge-collection {
  background: #e9ecef;
  color: var(--text-muted);
}

.badge-due {
  background: #d1ecf1;
  color: #0c5460;
}

.badge-due.overdue {
  background: #f8d7da;
  color: #721c24;
}

.badge-due.due-today {
  background: #fff3cd;
  color: #856404;
}

.badge-recurring {
  background: #d4edda;
  color: #155724;
}

.todo-notes {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 3px;
  white-space: pre-wrap;
}

.todo-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s;
}

.todo-item:hover .todo-actions {
  opacity: 1;
}

/* Login page */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 40px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.login-card h1 {
  font-size: 1.6rem;
  margin-bottom: 8px;
}

.login-card p {
  color: var(--text-muted);
  margin-bottom: 28px;
}

.btn-google {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 10px 20px;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
  width: 100%;
  justify-content: center;
  transition: background 0.15s, box-shadow 0.15s;
  color: var(--text);
  text-decoration: none;
}
.btn-google:hover {
  background: #f8f9fa;
  box-shadow: 0 1px 4px rgba(0,0,0,0.12);
  text-decoration: none;
}
.btn-google svg {
  flex-shrink: 0;
}

.error-msg {
  color: var(--danger);
  font-size: 0.875rem;
  margin-bottom: 12px;
}

/* Form */
.form-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 24px;
  max-width: 560px;
}

.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  margin-bottom: 5px;
}

.form-row {
  display: flex;
  gap: 12px;
}
.form-row .form-group {
  flex: 1;
}

.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}

.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
}
.checkbox-group input[type="checkbox"] {
  width: auto;
}

/* Collections page */
.collection-list {
  list-style: none;
}

.collection-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.collection-name {
  flex: 1;
  font-weight: 500;
}

.collection-edit-input {
  flex: 1;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state p {
  margin-bottom: 16px;
}

/* Loading */
.loading {
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
}

/* Todo section headers */
.todo-section-header {
  list-style: none;
  padding: 16px 2px 6px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
.todo-section-header:first-child {
  padding-top: 0;
}

/* Maintenance view */
.maintenance-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}
.maintenance-item:hover {
  border-color: #adb5bd;
}
.maintenance-item:hover .todo-actions {
  opacity: 1;
}
.maintenance-body {
  flex: 1;
  min-width: 0;
}
.maintenance-title {
  font-weight: 500;
  margin-bottom: 4px;
}
.maintenance-completion {
  margin-top: 6px;
}
.maintenance-last-done {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.maintenance-last-done.never {
  font-style: italic;
}
.maintenance-last-note {
  font-size: 0.875rem;
  color: var(--text);
  margin-top: 2px;
  white-space: pre-wrap;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}

.modal {
  background: var(--surface);
  border-radius: 8px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.modal-subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

/* Completion history */
.history-section {
  margin-top: 28px;
}

.history-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
}

.history-list {
  list-style: none;
}

.history-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.history-item:first-child {
  border-top: 1px solid var(--border);
}

.history-date {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.history-note {
  font-size: 0.9rem;
  white-space: pre-wrap;
}

.history-no-note {
  font-size: 0.9rem;
  color: var(--text-muted);
  font-style: italic;
}

/* ---- Mobile (iPhone portrait) ---- */
@media (max-width: 640px) {
  /* Sidebar → fixed bottom nav */
  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: stretch;
    border-right: none;
    border-top: 1px solid var(--border);
    /* Extend behind home indicator */
    padding-bottom: env(safe-area-inset-bottom);
  }

  .sidebar-header {
    display: none;
  }

  .sidebar-nav {
    display: flex;
    flex-direction: row;
    flex: 1;
    padding: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .sidebar-nav::-webkit-scrollbar {
    display: none;
  }

  .sidebar-nav li {
    flex-shrink: 0;
  }

  .sidebar-nav li a {
    height: 52px;
    padding: 0 18px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    font-size: 0.875rem;
    border-radius: 0;
  }

  .sidebar-section-label {
    display: none;
  }

  .sidebar-footer {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 0 12px;
    border-top: none;
    border-left: 1px solid var(--border);
    flex-shrink: 0;
    gap: 0;
  }
  .sidebar-footer .user-name {
    display: none;
  }

  /* Main content: full width, pad bottom for nav bar */
  .main-content {
    margin-left: 0;
    padding: 16px;
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
    max-width: 100%;
  }

  /* Todo actions: always visible (no hover on touch) */
  .todo-actions {
    opacity: 1;
    flex-direction: column;
    gap: 6px;
  }

  /* Larger complete button touch target */
  .todo-check button {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }

  /* Stack form rows vertically */
  .form-row {
    flex-direction: column;
  }

  .form-container {
    padding: 16px;
  }

  /* Prevent iOS zoom on input focus (requires >= 16px) */
  input[type="text"],
  input[type="number"],
  input[type="date"],
  textarea,
  select {
    font-size: 16px;
  }

  /* Login card: edge-to-edge on small screens */
  .login-card {
    border: none;
    border-radius: 0;
    padding: 40px 24px;
    max-width: 100%;
  }
}
