:root {
  --color-bg: #ffffff;
  --color-surface: #f9fafb;
  --color-border: #e5e7eb;
  --color-text: #111827;
  --color-text-secondary: #6b7280;
  --color-accent: #2563eb;
  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-error: #dc2626;
  --color-on-accent: #ffffff;
  --color-on-error: #ffffff;

  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;

  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;

  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.25rem;
  --font-size-2xl: 1.5rem;

  --max-width: 800px;
  --header-height: 3.5rem;
  --nav-height: 3.5rem;
}

:root[data-theme="dark"] {
  --color-bg: #111827;
  --color-surface: #1f2937;
  --color-border: #374151;
  --color-text: #f9fafb;
  --color-text-secondary: #9ca3af;
  --color-accent: #60a5fa;
  --color-success: #34d399;
  --color-warning: #fbbf24;
  --color-error: #f87171;
  --color-on-accent: #111827;
  --color-on-error: #111827;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #111827;
    --color-surface: #1f2937;
    --color-border: #374151;
    --color-text: #f9fafb;
    --color-text-secondary: #9ca3af;
    --color-accent: #60a5fa;
    --color-success: #34d399;
    --color-warning: #fbbf24;
    --color-error: #f87171;
    --color-on-accent: #111827;
    --color-on-error: #111827;
  }
}

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

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

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

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 var(--spacing-md);
  background-color: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.app-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
}

.app-body {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.view-container {
  flex: 1;
  width: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: var(--spacing-md);
}

/* In the login and in the wizard the navigation has no items. Without this it
   still paints its surface and its border: a strip on the side of the screen
   that means nothing to whoever is looking at it. */
.app-nav:empty {
  display: none;
}

.app-nav {
  position: sticky;
  bottom: 0;
  z-index: 100;
  display: flex;
  justify-content: space-around;
  height: var(--nav-height);
  background-color: var(--color-surface);
  border-top: 1px solid var(--color-border);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: var(--spacing-sm);
  color: var(--color-text-secondary);
  text-decoration: none;
  font-size: var(--font-size-sm);
  transition: color 0.15s;
}

.nav-item:hover,
.nav-item.active {
  color: var(--color-accent);
}

.nav-item svg {
  width: 1.5rem;
  height: 1.5rem;
  margin-bottom: var(--spacing-xs);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-sm) var(--spacing-md);
  font-size: var(--font-size-base);
  font-weight: 500;
  line-height: 1.5;
  text-align: center;
  text-decoration: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}

/* The theme button carries the three icons and CSS shows the one that matches
   the choice stored. Cycling through three states with a single icon made the
   step that follows the system look like nothing had happened, which is exactly
   what it looked like: with a dark system preference, 'system' and 'dark' paint
   the same. The attribute is set by theme-init.js before the first paint, so the
   right icon is the first one drawn. */
.theme-icon {
  display: none;
}

/* The last selector covers the browser where localStorage throws: theme-init.js
   sets no attribute there, and a button with no icon at all would be worse than
   one showing the default. */
:root[data-theme-choice="system"] .theme-icon-system,
:root[data-theme-choice="light"] .theme-icon-light,
:root[data-theme-choice="dark"] .theme-icon-dark,
:root:not([data-theme-choice]) .theme-icon-system {
  display: block;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background-color: var(--color-accent);
  color: var(--color-on-accent);
}

.btn-primary:hover:not(:disabled) {
  background-color: color-mix(in srgb, var(--color-accent) 90%, black);
}

.btn-secondary {
  background-color: transparent;
  border-color: var(--color-border);
  color: var(--color-text);
}

.btn-secondary:hover:not(:disabled) {
  background-color: var(--color-surface);
}

.btn-danger {
  background-color: var(--color-error);
  color: var(--color-on-error);
}

.btn-danger:hover:not(:disabled) {
  background-color: color-mix(in srgb, var(--color-error) 90%, black);
}

.btn-icon {
  padding: var(--spacing-sm);
  background: transparent;
  border: none;
  color: var(--color-text);
  cursor: pointer;
}

.btn-icon:hover {
  color: var(--color-accent);
}

.form-group {
  margin-bottom: var(--spacing-md);
}

.form-label {
  display: block;
  margin-bottom: var(--spacing-xs);
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--color-text);
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  font-size: var(--font-size-base);
  line-height: 1.5;
  color: var(--color-text);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: border-color 0.15s;
}

.form-input:focus-visible,
.form-select:focus-visible,
.form-textarea:focus-visible {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-accent) 30%, transparent);
}

.form-input:disabled,
.form-select:disabled,
.form-textarea:disabled {
  background-color: var(--color-surface);
  cursor: not-allowed;
}

.form-error {
  margin-top: var(--spacing-xs);
  font-size: var(--font-size-sm);
  color: var(--color-error);
}

.card {
  padding: var(--spacing-md);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}

.table-container {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-sm);
}

.table th,
.table td {
  padding: var(--spacing-sm) var(--spacing-md);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

.table th {
  font-weight: 600;
  color: var(--color-text-secondary);
  background-color: var(--color-surface);
}

.table tbody tr:hover {
  background-color: var(--color-surface);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-md);
  background-color: rgba(0, 0, 0, 0.5);
}

.modal {
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  background-color: var(--color-bg);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-md);
  border-bottom: 1px solid var(--color-border);
}

.modal-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
}

.modal-body {
  padding: var(--spacing-md);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--spacing-sm);
  padding: var(--spacing-md);
  border-top: 1px solid var(--color-border);
}

.toast-container {
  position: fixed;
  top: var(--spacing-md);
  right: var(--spacing-md);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.toast {
  padding: var(--spacing-md);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 250px;
  max-width: 400px;
}

.toast-success {
  border-left: 4px solid var(--color-success);
}

.toast-error {
  border-left: 4px solid var(--color-error);
}

.toast-warning {
  border-left: 4px solid var(--color-warning);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-xl);
  text-align: center;
  color: var(--color-text-secondary);
}

.empty-state svg {
  width: 4rem;
  height: 4rem;
  margin-bottom: var(--spacing-md);
  opacity: 0.5;
}

.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-xl);
  color: var(--color-text-secondary);
}

/* Exception to rule 7 (no animations): spinner uses rotation animation.
   Respects prefers-reduced-motion for accessibility. */
.spinner {
  width: 2rem;
  height: 2rem;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation: none;
  }
}

.login-container,
.setup-container,
.settings-container {
  max-width: 400px;
  margin: var(--spacing-xl) auto;
}

.login-title,
.setup-title,
.settings-title {
  font-size: var(--font-size-2xl);
  font-weight: 600;
  margin-bottom: var(--spacing-xs);
}

.login-subtitle,
.setup-subtitle,
.settings-subtitle {
  color: var(--color-text-secondary);
  margin-bottom: var(--spacing-lg);
}

.login-actions,
.setup-actions,
.settings-actions {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  margin-top: var(--spacing-lg);
}

.login-info {
  padding: var(--spacing-md);
  background-color: var(--color-surface);
  border-radius: var(--radius-md);
  margin-bottom: var(--spacing-md);
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
}

.login-resend {
  text-align: center;
  margin-top: var(--spacing-md);
}

@media (min-width: 640px) {
  .app-body {
    flex-direction: row;
  }

  .app-nav {
    /* Before the content, not after it: the nav is written last in the HTML so
       that on a phone it is the bottom bar, and on a wide screen a sidebar
       belongs on the left. */
    order: -1;
    position: static;
    flex-direction: column;
    /* space-around spread three items over the whole height of the column, which
       left them floating far apart. In a column they stack at the top. */
    justify-content: flex-start;
    gap: var(--spacing-xs);
    width: 200px;
    height: auto;
    padding: var(--spacing-sm);
    border-top: none;
    border-right: 1px solid var(--color-border);
  }

  .view-container {
    padding: var(--spacing-lg);
  }

  .nav-item {
    /* Not flex: 1 — that is what made each item as tall as a third of the
       screen. In a column an item is as tall as its content. */
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: flex-start;
    padding: var(--spacing-sm) var(--spacing-md);
    border-radius: var(--radius-md);
  }

  .nav-item:hover {
    background-color: var(--color-bg);
  }

  .nav-item svg {
    margin-bottom: 0;
    margin-right: var(--spacing-sm);
  }
}
