:root {
  --font-serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 20px;
  --radius-full: 999px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --fs-sm: 13px;
  --fs: 16px;
  --fs-lg: 20px;
  --fs-xl: 28px;
  --tap: 44px;
  --header-h: 60px;
  --home-gutter: 12px;
  --nav-h: 0px;
  --nav-total: 0px;
  --on-wallpaper: #FFFFFF;
  --on-wallpaper-shadow: 0 1px 3px rgba(0, 0, 0, 0.75);
}

:root,
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #FAF9F5;
  --surface: #FFFFFF;
  --surface-2: #F0EEE6;
  --border: #E5E2D8;
  --text: #1F1E1D;
  --text-muted: #6B6A66;
  --accent: #D97757;
  --accent-hover: #C6613F;
  --accent-contrast: #FFFFFF;
  --danger: #B3261E;
  --success: #3C7A4E;
  --overlay: rgba(31, 30, 29, 0.45);
  --wallpaper-scrim: rgba(31, 30, 29, 0.12);
  --shadow: 0 1px 2px rgba(31, 30, 29, 0.06), 0 8px 24px rgba(31, 30, 29, 0.08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #262624;
  --surface: #30302E;
  --surface-2: #3A3936;
  --border: #44433F;
  --text: #F2F0E8;
  --text-muted: #A8A59B;
  --accent: #D97757;
  --accent-hover: #E48A6C;
  --accent-contrast: #FFFFFF;
  --danger: #E5685F;
  --success: #6DBE85;
  --overlay: rgba(0, 0, 0, 0.6);
  --wallpaper-scrim: rgba(0, 0, 0, 0.35);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

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

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs);
}

[hidden] {
  display: none !important;
}

button {
  font: inherit;
  color: inherit;
}

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

.cos-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: 0 var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs);
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cos-btn:active {
  background: var(--surface-2);
}

.cos-btn:disabled,
.cos-btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

.cos-btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}

.cos-btn-primary:active {
  background: var(--accent-hover);
}

.cos-btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--accent-contrast);
  font-weight: 600;
}

.cos-btn-danger:active {
  filter: brightness(0.9);
  background: var(--danger);
}

.cos-btn-ghost {
  background: transparent;
  border-color: transparent;
}

.cos-btn-ghost:active {
  background: var(--surface-2);
}

.cos-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cos-icon-btn:active {
  background: var(--surface-2);
}

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

.cos-input {
  display: block;
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs);
  -webkit-appearance: none;
  appearance: none;
}

.cos-input::placeholder {
  color: var(--text-muted);
  opacity: 1;
}

.cos-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}

.cos-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.cos-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
}

.cos-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--header-h);
  padding: var(--space-2) 12px var(--space-2) var(--home-gutter);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.cos-title {
  min-width: 0;
  margin: 0;
  font-family: var(--font-serif);
  font-size: var(--fs-lg);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cos-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 48px;
  padding: var(--space-2) var(--space-4);
  border-bottom: 1px solid var(--border);
}

.cos-muted {
  color: var(--text-muted);
}

.cos-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--text);
  font-size: var(--fs-sm);
  line-height: 1.4;
}

.cos-segmented {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.cos-segmented button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: 0 var(--space-4);
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}

.cos-segmented button:last-child {
  border-right: 0;
}

.cos-segmented button:focus-visible {
  outline-offset: -2px;
}

.cos-segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
}

.cos-spinner {
  display: inline-block;
  flex: none;
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: cos-spin 0.8s linear infinite;
}

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

@keyframes cos-rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

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

.cos-dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  overflow: auto;
  background: var(--overlay);
}

.cos-dialog {
  width: 100%;
  max-width: 420px;
  max-height: 100%;
  overflow: auto;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.cos-dialog-title {
  margin: 0 0 var(--space-2);
  font-family: var(--font-serif);
  font-size: var(--fs-lg);
  font-weight: 600;
}

.cos-dialog-message {
  margin: 0 0 var(--space-4);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.cos-dialog-error {
  margin: var(--space-2) 0 0;
  color: var(--danger);
  font-size: var(--fs-sm);
  overflow-wrap: anywhere;
}

.cos-dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.cos-sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: var(--overlay);
}

.cos-sheet {
  width: 100%;
  max-width: 560px;
  max-height: 85%;
  overflow: auto;
  padding: var(--space-4) var(--space-4) calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.cos-sheet-title {
  margin: 0 0 var(--space-2);
  padding: 0 var(--space-3);
  font-family: var(--font-serif);
  font-size: var(--fs-lg);
  font-weight: 600;
  overflow-wrap: anywhere;
}

.cos-sheet-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.cos-sheet-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 48px;
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font-size: var(--fs);
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cos-sheet-item:active {
  background: var(--surface-2);
}

.cos-sheet-item-danger {
  color: var(--danger);
}

.cos-sheet-item:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.cos-sheet-icon {
  display: inline-flex;
  flex: none;
}

.cos-sheet-label {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.cos-sheet-cancel {
  width: 100%;
  margin-top: var(--space-3);
}

.cos-toast-host {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--space-5) + var(--nav-total, env(safe-area-inset-bottom, 0px)));
  z-index: 3000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
  pointer-events: none;
}

.cos-toast {
  max-width: 420px;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  line-height: 1.4;
  overflow-wrap: anywhere;
  animation: cos-rise 0.18s ease-out;
}

.cos-toast-error {
  background: var(--danger);
  color: var(--bg);
}

.cos-toast-success {
  background: var(--success);
  color: var(--bg);
}

.cos-busy {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: var(--overlay);
}

.cos-busy-box {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  max-width: 420px;
  padding: var(--space-4) 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.cos-busy-message {
  margin: 0;
  overflow-wrap: anywhere;
}

@media (hover: hover) {
  .cos-btn:hover {
    background: var(--surface-2);
  }

  .cos-btn-primary:hover {
    background: var(--accent-hover);
  }

  .cos-btn-danger:hover {
    filter: brightness(0.92);
    background: var(--danger);
  }

  .cos-btn-ghost:hover,
  .cos-icon-btn:hover,
  .cos-sheet-item:hover:not(:disabled) {
    background: var(--surface-2);
  }

  .cos-segmented button[aria-pressed="false"]:hover {
    background: var(--surface-2);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cos-spinner {
    animation-duration: 2s;
  }

  .cos-toast {
    animation: none;
  }
}
