:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --text: #3f3f42;
  --muted: #6e6e73;
  --line: #dedee3;
  --primary: #4a4a4d;
  --primary-hover: #606064;
  --on-primary: #ffffff;
  --soft-accent: #ececee;
  --danger: #d70015;
  --focus: #48484a;
  --toast-bg: #1d1d1f;
  --toast-text: #f5f5f7;
  --nav-bg: rgb(255 255 255 / 88%);
  --keyboard-inset: 0px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "PingFang SC", "Segoe UI", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --text: #f5f5f7;
    --muted: #98989d;
    --line: #38383a;
    --primary: #f5f5f7;
    --primary-hover: #d1d1d6;
    --on-primary: #1c1c1e;
    --soft-accent: #2c2c2e;
    --danger: #ff453a;
    --focus: #aeaeb2;
    --toast-bg: #f5f5f7;
    --toast-text: #1c1c1e;
    --nav-bg: rgb(28 28 30 / 88%);
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-height: 100%;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body {
  min-width: 280px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button,
input,
textarea {
  font: inherit;
}

button,
summary {
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}

button:hover:not(:disabled) {
  border-color: var(--muted);
}

button:disabled {
  cursor: default;
  opacity: 0.55;
}

button:focus-visible,
summary:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

input,
textarea {
  width: 100%;
  min-height: 44px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

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

input:focus,
textarea:focus {
  border-color: var(--focus);
}

textarea {
  min-height: 72px;
  line-height: 1.4;
  resize: vertical;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
}

h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.015em;
}

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

.primary-button {
  border-color: var(--primary);
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 650;
}

.primary-button:hover:not(:disabled) {
  border-color: var(--primary-hover);
  background: var(--primary-hover);
}

.full-button {
  width: 100%;
}

.text-button {
  border-color: transparent;
  background: transparent;
  color: var(--primary);
}

.text-button:hover:not(:disabled) {
  border-color: transparent;
  background: var(--soft-accent);
}

.danger-text,
.danger-button {
  color: var(--danger);
}

.icon-button {
  width: 44px;
  padding: 0;
  border-color: transparent;
  background: transparent;
  font-size: 28px;
  line-height: 1;
}

.field {
  min-width: 0;
  margin-bottom: 14px;
}

.field label {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 13px;
}

.form-error {
  margin: 4px 0 12px;
  color: var(--danger);
  font-size: 14px;
}

.auth-view {
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(36px + env(safe-area-inset-top)) 18px calc(28px + env(safe-area-inset-bottom));
}

.auth-content {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
}

.login-content {
  max-width: 420px;
  padding-top: 7vh;
}

.auth-intro {
  margin-bottom: 24px;
  color: var(--muted);
  font-size: 14px;
}

.account-fields {
  min-width: 0;
  margin: 0 0 20px;
  padding: 18px 0 2px;
  border: 0;
  border-top: 1px solid var(--line);
}

.account-fields legend {
  padding: 0 10px 0 0;
  font-weight: 650;
}

.account-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 20px;
}

.account-picker button {
  min-height: 48px;
  font-weight: 600;
}

.account-picker button[aria-pressed="true"] {
  border-color: var(--primary);
  background: var(--soft-accent);
  color: var(--primary);
}

#app-view {
  width: 100%;
  max-width: 720px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: calc(64px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 18px 0;
  border-bottom: 1px solid var(--line);
  background: var(--nav-bg);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}

.topbar h1 {
  margin: 0;
  font-size: 23px;
  font-weight: 700;
  letter-spacing: -0.025em;
}

.user-menu {
  position: relative;
  flex: none;
}

.user-menu summary {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px 0 14px;
  border-radius: 999px;
  background: var(--soft-accent);
  color: var(--text);
  cursor: pointer;
  list-style: none;
}

.user-menu summary::-webkit-details-marker {
  display: none;
}

.menu-chevron {
  margin-left: 5px;
  font-size: 17px;
}

.menu-popover {
  position: absolute;
  top: 48px;
  right: 0;
  z-index: 30;
  width: 196px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 10px 32px rgb(0 0 0 / 12%);
}

.menu-popover button {
  display: block;
  width: 100%;
  border-color: transparent;
  text-align: left;
}

#main-content {
  padding: 0 16px calc(24px + env(safe-area-inset-bottom) + var(--keyboard-inset));
}

.add-form {
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / 3%);
}

.add-kind-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin-bottom: 10px;
  padding: 3px;
  border-radius: 12px;
  background: var(--soft-accent);
}

.add-kind-picker button {
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}

.add-kind-picker button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgb(0 0 0 / 10%);
}

.add-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.add-row .primary-button {
  min-width: 64px;
}

.add-more {
  min-height: 44px;
  margin-top: 2px;
  padding: 0;
  font-size: 13px;
}

.extra-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 10px;
  padding-top: 4px;
}

.field-wide {
  grid-column: 1 / -1;
}

.inline-error {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 52px;
  padding: 4px 0;
  color: var(--danger);
  font-size: 14px;
}

.inline-error .text-button {
  flex: none;
}

.item-list,
.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.item-group {
  margin-top: 14px;
}

.group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  padding: 0 2px;
}

.group-header h2 {
  font-size: 18px;
  font-weight: 650;
}

#task-list,
#shopping-list,
#history-list {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / 3%);
}

#task-list,
#shopping-list {
  margin-top: 4px;
}

#task-list:empty,
#shopping-list:empty,
#history-list:empty {
  display: none;
}

.group-empty {
  margin: 0;
  padding: 4px 2px 10px;
  color: var(--muted);
  font-size: 14px;
}

.item-row {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  align-items: stretch;
  min-height: 64px;
  border-bottom: 1px solid var(--line);
}

.item-row:last-child {
  border-bottom: 0;
}

.check-button {
  display: grid;
  width: 48px;
  min-height: 56px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 0;
  background: transparent;
}

.check-button:hover:not(:disabled) {
  border: 0;
  background: var(--soft-accent);
}

.check-circle {
  display: grid;
  width: 23px;
  height: 23px;
  place-items: center;
  border: 2px solid var(--muted);
  border-radius: 50%;
  color: var(--on-primary);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
}

.item-open {
  min-width: 0;
  min-height: 62px;
  padding: 10px 14px 10px 2px;
  border: 0;
  border-radius: 0;
  background: transparent;
  text-align: left;
}

.item-open:hover:not(:disabled) {
  border: 0;
  background: var(--soft-accent);
}

.item-primary {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.item-name {
  min-width: 0;
  overflow-wrap: anywhere;
  font-weight: 600;
}

.item-quantity {
  flex: none;
  color: var(--muted);
  font-size: 14px;
  white-space: pre-wrap;
}

.item-kind {
  flex: none;
  padding: 1px 5px;
  border-radius: 6px;
  background: var(--soft-accent);
  color: var(--muted);
  font-size: 12px;
  font-weight: 550;
}

.item-meta {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.item-reminder {
  display: block;
  margin-bottom: 2px;
  color: var(--text);
  font-weight: 550;
}

.item-reminder.is-due {
  color: var(--danger);
}

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

.item-row.is-bought .check-circle {
  border-color: var(--primary);
  background: var(--primary);
}

.empty-note {
  margin: 0;
  padding: 24px 12px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

.bought-section {
  margin-top: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / 3%);
}

.bought-section > summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 52px;
  padding: 0 14px;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}

.bought-section > summary::-webkit-details-marker {
  display: none;
}

.bought-count {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.bought-count::after {
  margin-left: 8px;
  content: "›";
  font-size: 21px;
  line-height: 1;
  transition: transform 140ms ease;
}

.bought-section[open] .bought-count::after {
  transform: rotate(90deg);
}

.bought-list {
  border-top: 1px solid var(--line);
  opacity: 0.9;
}

.section-link {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 0 4px 0 12px;
  border-color: transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
}

.section-link:hover:not(:disabled) {
  border-color: transparent;
  background: var(--soft-accent);
}

.section-link span:last-child {
  font-size: 21px;
  font-weight: 400;
}

.history-header {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding-top: 4px;
}

.history-header h2 {
  white-space: nowrap;
}

.back-button {
  flex: none;
  padding: 0 8px 0 0;
  border-color: transparent;
  background: transparent;
  font-size: 17px;
  font-weight: 600;
}

.back-button:hover:not(:disabled) {
  border-color: transparent;
  background: var(--soft-accent);
}

.history-search {
  padding: 4px 0 0;
}

#history-list {
  margin-top: 12px;
}

.history-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 68px;
  padding: 10px 12px 10px 14px;
  border-bottom: 1px solid var(--line);
}

.history-row:last-child {
  border-bottom: 0;
}

.history-main {
  min-width: 0;
}

.history-name {
  overflow-wrap: anywhere;
  font-weight: 600;
}

.history-quantity {
  margin-left: 7px;
  color: var(--muted);
  font-size: 14px;
}

.history-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.history-delete {
  min-width: 54px;
  padding: 0 8px;
  border-color: transparent;
  background: transparent;
  color: var(--danger);
  font-size: 14px;
}

.bottom-sheet {
  position: fixed;
  inset: auto 0 var(--keyboard-inset) 0;
  width: 100%;
  max-width: 720px;
  max-height: calc(100vh - 20px - var(--keyboard-inset));
  max-height: calc(100dvh - 20px - var(--keyboard-inset));
  margin: 0 auto;
  padding: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
  animation: sheet-in 160ms ease-out;
}

.bottom-sheet::backdrop {
  background: rgb(0 0 0 / 42%);
}

.sheet-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  padding: 4px 10px 4px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.sheet-fields {
  padding: 16px 16px 0;
}

.sheet-two-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.sheet-note {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
}

.sheet-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px calc(14px + env(safe-area-inset-bottom));
}

.sheet-actions .primary-button {
  flex: 1;
}

.sheet-actions .danger-button {
  margin-left: auto;
}

#toast {
  position: fixed;
  right: 12px;
  bottom: calc(16px + env(safe-area-inset-bottom) + var(--keyboard-inset));
  left: 12px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: auto;
  max-width: 680px;
  min-height: 50px;
  margin: 0 auto;
  padding: 3px 6px 3px 14px;
  border-radius: 14px;
  background: var(--toast-bg);
  color: var(--toast-text);
  box-shadow: 0 6px 24px rgb(0 0 0 / 20%);
  font-size: 14px;
  animation: toast-in 140ms ease-out;
}

#toast-message {
  min-width: 0;
  padding: 8px 0;
  overflow-wrap: anywhere;
}

#toast-action {
  flex: none;
  min-width: 54px;
  border-color: transparent;
  background: transparent;
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

#connection-note {
  position: fixed;
  top: calc(5px + env(safe-area-inset-top));
  left: 50%;
  z-index: 60;
  max-width: calc(100% - 32px);
  padding: 4px 10px;
  transform: translateX(-50%);
  border-radius: 20px;
  background: var(--toast-bg);
  color: var(--toast-text);
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
}

.primary-nav {
  position: sticky;
  top: calc(64px + env(safe-area-inset-top));
  z-index: 19;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 7px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--nav-bg);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}

.primary-nav button {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}

.primary-nav button[aria-current="page"] {
  background: var(--soft-accent);
  color: var(--text);
}

.form-hint {
  margin: 6px 2px 0;
  color: var(--muted);
  font-size: 12px;
}

#board-image input[type="file"] {
  padding: 6px;
}

#board-image input[type="file"]::file-selector-button {
  min-height: 34px;
  margin-right: 10px;
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: var(--soft-accent);
  color: var(--text);
  font: inherit;
}

#board-image-preview {
  margin: 0 0 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--soft-accent);
}

#board-preview-image {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: contain;
}

.board-list {
  display: grid;
  gap: 14px;
  margin: 16px 0 0;
  padding: 0;
  list-style: none;
}

.board-list:empty {
  display: none;
}

.board-post {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / 3%);
}

.board-post-content {
  margin: 0;
  padding: 16px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.65;
}

.board-image-open {
  display: block;
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--soft-accent);
  cursor: zoom-in;
}

.board-image-open img {
  display: block;
  width: 100%;
  height: auto;
}

.board-caption {
  margin: 0;
  padding: 14px 16px 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.board-post-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
  padding: 8px 10px 8px 16px;
  color: var(--muted);
  font-size: 12px;
}

.board-delete {
  flex: none;
  min-height: 36px;
  padding: 0 8px;
  border-color: transparent;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
}

.image-viewer {
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  border: 0;
  background: var(--bg);
  color: var(--text);
}

.image-viewer::backdrop {
  background: var(--bg);
}

.image-viewer-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: calc(56px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 10px 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--nav-bg);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  font-weight: 600;
}

#board-full-image {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

@keyframes sheet-in {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (min-width: 721px) {
  .auth-view {
    padding-top: 56px;
  }

  .bottom-sheet {
    bottom: max(12px, var(--keyboard-inset));
    border-bottom: 1px solid var(--line);
    border-radius: 20px;
  }

  .sheet-actions {
    padding-bottom: 16px;
  }
}

@media (max-width: 340px) {
  #main-content,
  .topbar {
    padding-right: 12px;
    padding-left: 12px;
  }

  .item-quantity {
    max-width: 76px;
    white-space: normal;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
