/* テーマのカラートークンは /theme.css (app/services/theme.py) で生成する。
   層の明度差と文字コントラストはそちらで検証している。 */

* { box-sizing: border-box; }
body {
  font-family: system-ui, sans-serif;
  max-width: 1200px;
  margin: 0 auto;
  padding: 1em;
  background: var(--page-bg);
  color: var(--text);
}
a { color: var(--accent); }
nav {
  display: flex;
  gap: 1em;
  align-items: center;
  border-bottom: 2px solid var(--border);
  padding-bottom: .75em;
  margin-bottom: 1em;
}
nav a { text-decoration: none; color: var(--text); }
nav a[aria-current="page"] { font-weight: 800; }
#theme-toggle, button {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: .55em .9em;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
#theme-toggle { margin-left: auto; min-width: 44px; }
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
table { border-collapse: collapse; width: 100%; color: var(--text); }
th, td { border: 1px solid var(--border); padding: .65em .75em; text-align: left; }
th { background: var(--surface-raised); }
td { background: var(--surface); }
input, select, textarea {
  max-width: 100%;
  min-height: 44px;
  padding: .55em .7em;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-raised);
  color: var(--text);
  font: inherit;
}
textarea { min-height: 6em; }
.flashes {
  background: var(--flash-bg);
  border: 2px solid var(--flash-border);
  border-radius: 7px;
  color: var(--flash-text);
  padding: .75em 1em;
  list-style: none;
}

@media print {
  :root, :root[data-theme="light"], :root[data-theme="dark"] {
    color-scheme: light;
    --page-bg: #fff;
    --surface: #fff;
    --surface-raised: #fff;
    --border: #333;
    --text: #000;
    --muted: #000;
  }
  body { max-width: none; background: #fff; color: #000; }
  nav, form, .flashes, .no-print, #theme-toggle { display: none; }
}

#ocr-progress {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 999;
  align-items: center; justify-content: center;
}
#ocr-progress .box {
  background: var(--surface-raised); color: var(--text); padding: 2em 3em; border-radius: 8px; text-align: center;
}
#ocr-progress .spinner {
  width: 40px; height: 40px; margin: 0 auto 1em;
  border: 4px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: ocr-spin .8s linear infinite;
}
@keyframes ocr-spin { to { transform: rotate(360deg); } }

[hidden] { display: none !important; }
.site-header { display: flex; align-items: center; justify-content: space-between; gap: 1em; padding: .5em 0; }
.brand { color: var(--text); font-size: 1.25rem; font-weight: 800; text-decoration: none; }
.site-header .brand { margin-right: auto; }
#theme-toggle, #nav-toggle { margin: 0; }
#nav-toggle { display: none; }
.desktop-nav { gap: 1em; }
.drawer-backdrop {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, .5);
  opacity: 0; transition: opacity .2s ease;
}
.drawer-backdrop { pointer-events: none; }
.drawer-backdrop.is-open { opacity: 1; pointer-events: auto; }
.site-drawer {
  position: fixed; z-index: 1001; inset: 0 auto 0 0;
  width: min(84vw, 340px); overflow-y: auto;
  background: var(--surface); color: var(--text);
  border-right: 2px solid var(--border);
  transform: translateX(-100%); transition: transform .2s ease;
}
.site-drawer.is-open { transform: translateX(0); }
.drawer-header { display: flex; align-items: center; justify-content: space-between; padding: 1em; background: var(--surface-raised); }
.drawer-links { display: flex; flex-direction: column; gap: 0; border: 0; margin: 0; padding: .5em; }
.drawer-links a { display: flex; align-items: center; min-height: 52px; padding: .75em; border-bottom: 1px solid var(--border); color: var(--text); text-decoration: none; }
body.drawer-open { overflow: hidden; }
@media (max-width: 768px) {
  body { padding: .75em; }
  #nav-toggle { display: inline-flex; align-items: center; justify-content: center; }
  #desktop-nav { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .site-drawer, .drawer-backdrop { transition: none; }
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1em;
  max-width: 900px;
}
.form-grid > .field { min-width: 0; margin: 0; }
.form-grid > .form-wide, .form-grid > h2 { grid-column: 1 / -1; }
.field > label { display: block; font-weight: 650; }
.field input:not([type="checkbox"]), .field select, .field textarea { width: 100%; margin-top: .35em; }
.field input[type="checkbox"] { width: 1.25em; height: 1.25em; min-height: 0; margin-right: .5em; vertical-align: middle; }
.checkbox-field { align-self: end; }
.actions { display: flex; gap: .75em; align-items: center; }
.button-primary { min-width: 9em; }
.form-table-wrap { grid-column: 1 / -1; max-width: 100%; overflow-x: auto; }
.form-table-wrap table { min-width: 620px; }
.form-table-wrap input { min-width: 8em; }
.scan-form {
  max-width: 760px;
  margin: 0 0 1.5em;
  padding: 1em;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
}
.scan-form > p { margin: 0; }
.scan-form select, .scan-form input[type="file"] { width: 100%; }
.scan-form > button { grid-column: 1 / -1; width: 100%; }
h1, h2, h3 { color: var(--text); }
.estimate-total[readonly] { background: var(--surface); border-style: dashed; font-weight: 700; }
#estimate-total-hint { display: block; margin-top: .35em; color: var(--muted); }

@media (max-width: 768px) {
  .form-grid { grid-template-columns: minmax(0, 1fr); gap: .9em; }
  .form-grid > .form-wide, .form-grid > h2 { grid-column: 1; }
  .actions { flex-direction: column; align-items: stretch; }
  .actions button { width: 100%; }
  .field input[type="file"] { padding: .45em; }
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1em;
}
.metric-card {
  min-width: 0;
  padding: 1.1em;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 9px;
}
.metric-card h2 { margin: 0 0 .6em; font-size: 1rem; }
.metric-value { margin: 0 0 .5em; font-size: clamp(1.25rem, 2.5vw, 1.8rem); font-weight: 800; overflow-wrap: anywhere; }
.metric-sub { margin: 0 0 .5em; color: var(--muted); font-size: .9rem; }
.metric-card.attention { border-color: var(--flash-border); background: var(--flash-bg); color: var(--flash-text); }
.metric-card.attention h2, .metric-card.attention a { color: var(--flash-text); }
.metric-card.attention .metric-sub { color: var(--flash-text); }
.dashboard-actions { display: flex; flex-wrap: wrap; gap: .75em; margin-top: 1.25em; }
.dashboard-actions .button-primary { display: inline-flex; align-items: center; justify-content: center; min-height: 48px; padding: .7em 1em; border-radius: 7px; background: var(--accent); color: var(--accent-text); font-weight: 800; text-decoration: none; }
@media (max-width: 768px) {
  .dashboard-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7em; }
  .metric-card { padding: .9em; }
  .dashboard-actions { flex-direction: column; }
  .dashboard-actions .button-primary { width: 100%; }
}
@media (max-width: 420px) {
  .dashboard-grid { grid-template-columns: minmax(0, 1fr); }
}

.filter-form { display: flex; flex-wrap: wrap; align-items: center; gap: .7em; margin: 1em 0; }
.inline-delete { display: inline-flex; margin: 0; }
.row-actions { white-space: nowrap; }
.gift-inline-form { display: flex; flex-wrap: wrap; align-items: end; gap: .65em; }
.gift-field { display: flex; flex-direction: column; gap: .25em; font-size: .88rem; }
.gift-field input, .gift-field select { min-width: 8em; }
.table-total { margin-top: 1em; padding: .85em 1em; background: var(--surface); border: 1px solid var(--border); border-radius: 7px; }
.table-scroll { width: 100%; max-width: 100%; overflow-x: auto; overscroll-behavior-x: contain; }
.table-scroll table { min-width: max-content; }
.table-scroll th, .table-scroll td { white-space: nowrap; }

@media (max-width: 768px) {
  .filter-form { display: grid; grid-template-columns: minmax(0, 1fr); }
  .filter-form input, .filter-form select, .filter-form button { width: 100%; }
  .responsive-list, .responsive-list tbody { display: block; width: 100%; border: 0; }
  .responsive-list thead {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
  }
  .responsive-list tr {
    display: block; margin: 0 0 1em; overflow: hidden;
    background: var(--surface); border: 2px solid var(--border); border-radius: 9px;
  }
  .responsive-list td {
    display: grid; grid-template-columns: minmax(6.5em, 34%) minmax(0, 1fr);
    gap: .75em; width: 100%; padding: .75em; border: 0;
    border-bottom: 1px solid var(--border); background: transparent;
    overflow-wrap: anywhere; min-width: 0;
  }
  .responsive-list td:last-child { border-bottom: 0; }
  .responsive-list td::before { content: attr(data-label); color: var(--muted); font-weight: 800; }
  .responsive-list .row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: .75em; white-space: normal; }
  .responsive-list .row-actions::before { flex-basis: 100%; }
  .responsive-list .inline-delete { display: inline-flex; }
  .responsive-list .gift-cell { display: block; }
  .responsive-list .gift-cell::before { display: block; margin-bottom: .6em; }
  .gift-inline-form { display: grid; grid-template-columns: minmax(0, 1fr); align-items: stretch; gap: .7em; }
  .gift-field input, .gift-field select, .gift-inline-form button { width: 100%; min-width: 0; }
  .calc-method { color: var(--muted); }
}

.mourners-list .guest-name { font-weight: 800; }
.mourners-list .guest-name small { display: block; color: var(--muted); font-weight: 400; }
.mourners-list .guest-meta { color: var(--muted); }
.mourners-list .meta-item:not(:last-child)::after { content: " · "; white-space: pre; }
.mourners-list .guest-amount { font-weight: 800; white-space: nowrap; }
.gift-status { display: inline-block; padding: .2em .55em; border-radius: 999px; background: var(--surface-raised); font-size: .86em; font-weight: 700; white-space: nowrap; }
.gift-status.status-pending { background: var(--flash-bg); color: var(--flash-text); }
.mourners-list .row-actions { white-space: nowrap; }
@media (max-width: 768px) {
  .mourners-list tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "name amount" "meta status" "actions actions";
    gap: .2em .75em;
    margin: 0;
    padding: .65em .25em;
    border: 0;
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    background: transparent;
    overflow: visible;
  }
  .mourners-list td { display: block; width: auto; min-width: 0; padding: .2em 0; border: 0; background: transparent; }
  .mourners-list td::before { display: none; }
  .mourners-list .guest-name { grid-area: name; overflow-wrap: anywhere; }
  .mourners-list .guest-meta { grid-area: meta; display: flex; flex-wrap: wrap; gap: .1em; font-size: .82rem; }
  .mourners-list .guest-amount { grid-area: amount; align-self: start; text-align: right; font-size: 1.05rem; }
  .mourners-list .guest-status { grid-area: status; justify-self: end; align-self: start; }
  .mourners-list .row-actions { grid-area: actions; display: flex; justify-content: flex-end; gap: .75em; white-space: normal; }
  .mourners-list .row-actions::before { display: none; }
}

.save-fab { white-space: nowrap; }
@media (max-width: 768px) {
  .save-action-form { padding-bottom: calc(6rem + env(safe-area-inset-bottom)); }
  .save-action-form .save-fab {
    position: fixed;
    right: calc(1rem + env(safe-area-inset-right));
    bottom: calc(1rem + env(safe-area-inset-bottom));
    z-index: 900;
    min-width: 7rem;
    min-height: 56px;
    padding: .75rem 1.25rem;
    border-radius: 999px;
    box-shadow: 0 3px 12px rgba(0, 0, 0, .3);
  }
}
@media print { .save-fab { display: none; } }
.estimate-total[readonly] { background: var(--surface); border-style: dashed; font-weight: 700; }
#estimate-total-hint { display: block; margin-top: .35em; color: var(--muted); }

.form-table-wrap .members-table { min-width: 0; }
.members-table input:not([type="checkbox"]) { width: 100%; min-width: 0; }
.attendance-cell { display: flex; flex-wrap: wrap; gap: .4em .9em; }
.attendance-option { display: inline-flex; align-items: center; gap: .3em; white-space: nowrap; }
.attendance-option input[type="checkbox"] { width: 1.25em; height: 1.25em; min-width: 0; min-height: 0; margin: 0; }

.balance-table { max-width: 460px; }
.balance-table th:last-child, .balance-table td:last-child { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

.postal-status { display: block; margin-top: .25em; color: var(--muted); font-size: .85em; }
.balance-table tr.balance-total td { background: var(--surface-raised); font-weight: 800; }
.postal-candidate { margin: .2em .3em 0 0; min-height: 0; padding: .25em .6em; font-size: .85em; }
.back-button { margin-bottom: 1em; }

.logout-form { margin: 0; }
.logout-button { min-height: 44px; padding: .4em .8em; font-size: .9rem; }
.login-page { max-width: 420px; margin: 8vh auto; }
.login-form { display: grid; gap: .9em; }
.login-form input { width: 100%; }
.login-form button { min-height: 48px; }
