:root {
  color-scheme: light dark;
  --brand-red: #bd1e25;
  --brand-red-pressed: #94171c;
  --brand-red-soft: #f9e8e9;
  --brand-green: #14783b;
  --brand-green-soft: #e6f3ea;
  --canvas: #f4f2ef;
  --surface: #fffdf9;
  --surface-muted: #f1efeb;
  --text: #211d1c;
  --text-muted: #696260;
  --border: #d8d2ce;
  --error: #a1161d;
  --error-soft: #fae8e9;
  --focus: #8b1a20;
  --disabled: #d8d2ce;
  --disabled-text: #77706d;
  --shadow: 0 12px 30px rgba(64, 32, 29, 0.1);
  --radius: 14px;
  --control-radius: 10px;
  --transition: 180ms cubic-bezier(0.22, 1, 0.36, 1);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--canvas);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  background: var(--canvas);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
}

::selection {
  background: var(--brand-red);
  color: #fff;
}

button,
input,
select,
textarea {
  max-width: 100%;
  font: inherit;
  font-size: 16px;
}

button,
select,
input[type="checkbox"] {
  cursor: pointer;
}

button:disabled,
select:disabled,
input:disabled {
  cursor: not-allowed;
}

button,
input,
select,
textarea {
  border-radius: var(--control-radius);
}

input,
select,
textarea {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  caret-color: var(--brand-red);
  padding: 11px 12px;
  transition: border-color var(--transition), box-shadow var(--transition), background-color var(--transition);
}

textarea {
  min-height: 92px;
  resize: vertical;
}

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

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 32%, transparent);
  outline-offset: 2px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 1px var(--focus);
}

input:disabled,
select:disabled {
  background: var(--surface-muted);
  color: var(--disabled-text);
  opacity: 1;
}

.app-shell,
.login-shell {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  background: var(--surface);
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
}

.login-shell {
  display: grid;
  min-height: 100vh;
  min-height: 100dvh;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}

.login-panel {
  width: min(400px, calc(100vw - 40px));
  min-width: 0;
}

.brand-logo {
  display: block;
  width: min(100%, 236px);
  height: auto;
  object-fit: contain;
}

.brand-logo-login {
  width: min(100%, 290px);
  margin-bottom: 36px;
}

.login-heading {
  margin-bottom: 28px;
}

.login-heading h1,
.panel-heading h1 {
  margin: 0 0 6px;
  font-size: 1.5rem;
  line-height: 1.25;
  letter-spacing: -0.02em;
}

.login-heading p,
.panel-heading p,
.privacy-note,
.teacher-session p {
  margin: 0;
  color: var(--text-muted);
}

.privacy-note {
  margin-top: 24px;
  font-size: 0.875rem;
}

.app-header {
  display: flex;
  min-height: 92px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: max(16px, env(safe-area-inset-top)) 18px 14px;
  border-bottom: 1px solid var(--border);
}

.app-header .brand-logo {
  width: min(54%, 220px);
}

.teacher-session {
  flex: 0 0 auto;
  text-align: right;
}

.teacher-session p {
  font-size: 0.8125rem;
}

.teacher-session strong {
  display: block;
  color: var(--text);
  font-size: 0.9375rem;
}

.text-button {
  min-height: 44px;
  margin: 2px -10px -8px 0;
  border: 0;
  background: transparent;
  color: var(--brand-red);
  padding: 8px 10px;
  font-weight: 650;
}

.action-switcher {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  position: sticky;
  z-index: 10;
  top: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 0 10px;
}

.action-switcher button {
  position: relative;
  min-height: 52px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 5px;
  font-size: 0.9375rem;
  font-weight: 650;
}

.action-switcher button::after {
  position: absolute;
  right: 8px;
  bottom: -1px;
  left: 8px;
  height: 3px;
  border-radius: 3px 3px 0 0;
  background: transparent;
  content: "";
}

.action-switcher button[aria-selected="true"] {
  color: var(--brand-red);
}

.action-switcher button[aria-selected="true"]::after {
  background: var(--brand-red);
}

.workspace {
  padding: 24px 18px max(40px, env(safe-area-inset-bottom));
}

.task-panel[hidden],
[hidden] {
  display: none !important;
}

.panel-heading {
  margin-bottom: 28px;
}

.panel-heading p {
  font-size: 0.9375rem;
}

.form-stack {
  display: grid;
  gap: 24px;
}

.field-group,
.search-block {
  min-width: 0;
}

.field-group > label,
.search-block > label,
.field-group legend {
  display: block;
  margin: 0 0 8px;
  color: var(--text);
  font-size: 0.9375rem;
  font-weight: 700;
}

fieldset.field-group {
  min-width: 0;
  margin: 0;
  border: 0;
  padding: 0;
}

.sub-label {
  margin-top: 12px !important;
  color: var(--text-muted) !important;
  font-size: 0.875rem !important;
  font-weight: 600 !important;
}

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

.search-row .secondary-button {
  min-width: 68px;
}

.search-feedback {
  margin-top: 7px;
  color: var(--text-muted);
  font-size: 0.8125rem;
}

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

.search-results {
  display: grid;
  position: relative;
  z-index: 2;
  gap: 1px;
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: var(--control-radius);
  overflow: hidden;
  background: var(--border);
}

.search-result {
  display: flex;
  width: 100%;
  min-height: 56px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 0;
  border-radius: 0;
  background: var(--surface);
  color: var(--text);
  padding: 10px 12px;
  text-align: left;
}

.search-result:hover,
.search-result:active {
  background: var(--brand-red-soft);
}

.search-result strong,
.search-result span {
  display: block;
}

.search-result span,
.search-result small {
  color: var(--text-muted);
}

.search-result small {
  flex: 0 0 auto;
}

.selected-record {
  margin-top: 10px;
  border: 1px solid color-mix(in srgb, var(--brand-green) 32%, var(--border));
  border-radius: var(--control-radius);
  background: var(--brand-green-soft);
  color: var(--text);
  padding: 11px 12px;
}

.selected-record strong,
.selected-record span {
  display: block;
}

.selected-record span {
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.primary-button,
.secondary-button,
.choice-button,
.reason-button {
  min-height: 44px;
  border: 1px solid transparent;
  padding: 10px 14px;
  font-weight: 700;
  transition: background-color var(--transition), border-color var(--transition), color var(--transition), transform var(--transition);
}

.primary-button {
  min-height: 52px;
  background: var(--brand-red);
  color: #fff;
}

.primary-button:hover:not(:disabled),
.primary-button:active:not(:disabled) {
  background: var(--brand-red-pressed);
}

.primary-button:active:not(:disabled),
.secondary-button:active:not(:disabled),
.choice-button:active:not(:disabled),
.reason-button:active:not(:disabled) {
  transform: translateY(1px);
}

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

.secondary-button,
.choice-button,
.reason-button {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
}

.secondary-button:hover,
.choice-button:hover,
.reason-button:hover {
  border-color: var(--brand-red);
}

.choice-button[aria-pressed="true"] {
  border-color: var(--brand-red);
  background: var(--brand-red-soft);
  color: var(--brand-red-pressed);
}

.choice-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.reason-shortcuts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.reason-button {
  min-height: 44px;
  border-radius: 999px;
  padding: 8px 13px;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 600;
}

.summary-strip,
.balance-overview {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin: 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--surface-muted);
}

.summary-strip-three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.summary-strip > div,
.balance-overview > div {
  min-width: 0;
  padding: 13px 10px;
}

.summary-strip > div + div,
.balance-overview > div + div {
  border-left: 1px solid var(--border);
}

.summary-strip dt,
.balance-overview dt {
  margin-bottom: 4px;
  color: var(--text-muted);
  font-size: 0.75rem;
}

.summary-strip dd,
.balance-overview dd {
  margin: 0;
  overflow-wrap: anywhere;
  color: var(--text);
  font-size: 0.9375rem;
  font-variant-numeric: tabular-nums;
  font-weight: 750;
}

.summary-strip dd.is-danger {
  color: var(--error);
}

.delivery-confirmation {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  min-height: 60px;
  align-items: start;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: var(--control-radius);
  padding: 12px;
  cursor: pointer;
}

.delivery-confirmation:has(input:checked) {
  border-color: var(--brand-green);
  background: var(--brand-green-soft);
}

.delivery-confirmation input {
  width: 24px;
  height: 24px;
  min-height: 24px;
  margin: 1px 0 0;
  accent-color: var(--brand-green);
}

.delivery-confirmation strong,
.delivery-confirmation small {
  display: block;
}

.delivery-confirmation small {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.status-message {
  min-height: 44px;
  margin: 0;
  border-radius: var(--control-radius);
  background: var(--surface-muted);
  color: var(--text-muted);
  padding: 11px 12px;
  font-size: 0.875rem;
}

.status-message.is-loading {
  color: var(--text);
}

.status-message.is-error {
  background: var(--error-soft);
  color: var(--error);
}

.status-message.is-success {
  background: var(--brand-green-soft);
  color: var(--brand-green);
}

.balance-content {
  display: grid;
  gap: 28px;
}

.balance-overview {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.balance-overview .balance-primary {
  background: var(--brand-red-soft);
}

.balance-overview dd {
  font-size: 1.25rem;
}

.history-section h2 {
  margin: 0 0 10px;
  font-size: 1rem;
  line-height: 1.4;
}

.history-list {
  margin: 0;
  border-top: 1px solid var(--border);
  padding: 0;
  list-style: none;
}

.history-item,
.history-empty {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  padding: 12px 2px;
}

.history-item strong,
.history-item span,
.history-empty {
  font-size: 0.875rem;
}

.history-item span,
.history-empty {
  color: var(--text-muted);
}

.history-detail {
  display: grid;
  gap: 2px;
}

.history-item b {
  align-self: center;
  color: var(--text);
  font-size: 0.9375rem;
  font-variant-numeric: tabular-nums;
}

.history-empty {
  display: block;
}

[aria-busy="true"] {
  cursor: wait;
}

@media (hover: hover) {
  .text-button:hover {
    color: var(--brand-red-pressed);
    text-decoration: underline;
    text-underline-offset: 3px;
  }
}

@media (min-width: 760px) {
  body {
    padding: 28px 0;
  }

  .app-shell,
  .login-shell {
    border-radius: var(--radius);
    box-shadow: var(--shadow);
  }

  .app-shell {
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);
    overflow: clip;
  }

  .login-shell {
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);
  }
}

@media (max-width: 350px) {
  .app-header,
  .workspace {
    padding-right: 14px;
    padding-left: 14px;
  }

  .app-header .brand-logo {
    width: 50%;
  }

  .action-switcher button {
    font-size: 0.875rem;
  }

  .summary-strip-three dt {
    font-size: 0.6875rem;
  }

  .summary-strip-three dd {
    font-size: 0.8125rem;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand-red: #e24a50;
    --brand-red-pressed: #f06167;
    --brand-red-soft: #3d2022;
    --brand-green: #72c58d;
    --brand-green-soft: #183426;
    --canvas: #141211;
    --surface: #201d1c;
    --surface-muted: #2a2624;
    --text: #f5efeb;
    --text-muted: #c3bab5;
    --border: #4e4743;
    --error: #ff8b90;
    --error-soft: #441f22;
    --focus: #ff777d;
    --disabled: #3b3734;
    --disabled-text: #a49b96;
    --shadow: 0 12px 30px rgba(0, 0, 0, 0.3);
  }

  .brand-logo {
    border-radius: 8px;
    background: #fff;
    padding: 4px;
  }
}

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