:root {
  color-scheme: light;
  --bg: #f6f6f3;
  --surface: #ffffff;
  --text: #1b1d1f;
  --muted: #565b60;
  --border: #d4d6d2;
  --accent: #1f6f43;
  --on-accent: #ffffff;
  --accent-soft: #e2f1e8;
  --error: #b3261e;
  --warn-bg: #fff4d6;
  --warn-text: #5c4300;
  --highlight: #fff1b8;
  --snackbar-bg: #26292c;
  --snackbar-text: #f2f3f4;
  --snackbar-action: #8fe0b0;
  --focus: #1757c2;
  --count-selected-bg: rgb(255 255 255 / 0.22);
  --radius: 0.75rem;
  --pad-x: max(1rem, env(safe-area-inset-left), env(safe-area-inset-right));
}

/* The effective theme is set on <html data-theme> by js/theme.js before first paint:
   the saved override, or the system preference when there is none. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14181c;
  --surface: #1e2328;
  --text: #e8eaed;
  --muted: #a9b0b7;
  --border: #3a4148;
  --accent: #6fcf97;
  --on-accent: #0b2716;
  --accent-soft: #1f3a2b;
  --error: #ff8a80;
  --warn-bg: #4a3a0b;
  --warn-text: #ffe08a;
  --highlight: #5a4c12;
  --snackbar-bg: #e8eaed;
  --snackbar-text: #14181c;
  --snackbar-action: #1f6f43;
  --focus: #8ab4ff;
  --count-selected-bg: rgb(11 39 22 / 0.16);
}

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

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

body {
  margin: 0 auto;
  max-width: 36rem;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 0 var(--pad-x) calc(6rem + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.4;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* Header */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-template-areas:
    "title theme logout"
    "status status status";
  align-items: center;
  gap: 0.5rem;
  padding: calc(0.75rem + env(safe-area-inset-top)) 0 0.75rem;
}

.topbar h1 {
  grid-area: title;
  margin: 0;
  font-size: 1.375rem;
  white-space: nowrap;
}

.logout {
  grid-area: logout;
}

.connection {
  grid-area: status;
  justify-self: start;
  margin: 0;
  padding: 0.25rem 0.625rem;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 0.875rem;
  font-weight: 600;
}

.logout {
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
}

/* Theme toggle: the icon shows the action available (moon = switch to dark, sun = switch to light). */
.theme-toggle {
  grid-area: theme;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
}

.theme-toggle svg {
  display: block;
}

.theme-toggle .icon-sun,
:root[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}


/* Login */
.login form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 2rem;
  padding: 1.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.login label {
  font-weight: 600;
}

.error {
  min-height: 1.4em;
  margin: 0;
  color: var(--error);
}

input[type="password"],
input[type="text"],
textarea {
  width: 100%;
  min-height: 48px;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--muted);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 1rem; /* 16px+ prevents iOS zoom on focus */
}

textarea {
  display: block;
  margin: 0;
  line-height: 1.4;
  resize: vertical;
}

button[type="submit"] {
  min-height: 48px;
  padding: 0 1.25rem;
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

button[type="submit"]:disabled {
  opacity: 0.6;
  cursor: progress;
}

/* Tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.25rem;
  padding: 0.25rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}

.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  min-height: 44px;
  padding: 0 0.25rem;
  border: 0;
  border-radius: calc(var(--radius) - 0.25rem);
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}

.tab[aria-selected="true"] {
  background: var(--accent);
  color: var(--on-accent);
}

.count {
  min-width: 1.5em;
  padding: 0 0.375rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}

.tab[aria-selected="true"] .count {
  background: var(--count-selected-bg);
  color: inherit;
}

/* Narrow phones: stack the label above the count so four tabs fit at 320px. */
@media (max-width: 30rem) {
  .tab {
    flex-direction: column;
    gap: 0.125rem;
    min-height: 52px;
    padding: 0.25rem 0.125rem;
    font-size: 0.875rem;
  }
}

/* Add form */
.add-form {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-top: 1rem;
}

.add-form textarea {
  flex: 1;
  min-width: 0;
}

.feedback {
  min-height: 1.4em;
  margin: 0.375rem 0 0.25rem;
  color: var(--muted);
  font-size: 0.9375rem;
}

.feedback.is-error {
  color: var(--error);
}

/* List */
.list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.list li + li {
  margin-top: 0.5rem;
}

.item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  min-height: 52px;
  padding: 0.625rem 0.875rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: left;
  font-size: 1.0625rem;
  transition: background-color 150ms ease-out;
}

.item-name {
  overflow-wrap: anywhere;
}

.item-x {
  flex: none;
  color: var(--muted);
  font-size: 1.375rem;
  line-height: 1;
}

.item.is-highlighted {
  background: var(--highlight);
}

@media (hover: hover) {
  .item:hover {
    border-color: var(--muted);
  }
}

/* Notes */
.note-form {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.5rem;
  margin-top: 1rem;
}

.note-form button[type="submit"] {
  align-self: flex-end;
}

.notes {
  margin: 0;
  padding: 0;
  list-style: none;
}

.notes li + li {
  margin-top: 0.5rem;
}

.note {
  padding: 0.75rem 0.875rem 0.375rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.note-body {
  margin: 0;
  font-size: 1.0625rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.375rem;
}

.note-actions button {
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
}

.note-actions button.is-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}

.note-actions button:disabled {
  opacity: 0.6;
  cursor: progress;
}

.empty {
  margin: 2rem 0;
  color: var(--muted);
  text-align: center;
}

/* Snackbar */
.snackbar {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: min(34rem, calc(100% - 2 * var(--pad-x)));
  min-height: 52px;
  padding: 0.25rem 0.375rem 0.25rem 1rem;
  border-radius: var(--radius);
  background: var(--snackbar-bg);
  color: var(--snackbar-text);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.25);
  transform: translateX(-50%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease-out;
}

.snackbar.is-open {
  opacity: 1;
  pointer-events: auto;
}

#snackbar-text {
  flex: 1;
  overflow-wrap: anywhere;
}

#undo {
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.875rem;
  border: 0;
  border-radius: calc(var(--radius) - 0.25rem);
  background: transparent;
  color: var(--snackbar-action);
  font-weight: 700;
}

@media (min-width: 40rem) {
  .topbar {
    padding-top: calc(1.5rem + env(safe-area-inset-top));
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
