/* ==================== 主题变量 ==================== */
:root,
.theme-festive {
  --primary-color: #c00;
  --primary-color-hover: #a00;
  --primary-text-color: #c00;
  --primary-bg-light: #fff2f2;
  --primary-border-color: #ffaaaa;
  --primary-ring-color: #ef4444;
  --header-text-color: #b91c1c;
  --button-bg-color: #dc2626;
  --button-bg-hover: #b91c1c;
  --button-border-color: #dc2626;
  --button-text-color: #dc2626;
  --link-hover-bg: #fee2e2;
  --total-text-color: #ef4444;

  --name-font-size: 25px;
  --name-color: #333;
  --name-font-family: "KaiTi", "楷体", serif;
  --type-font-size: 22px;
  --type-color: #c00;
  --type-font-family: "KaiTi", "楷体", serif;
  --amount-chinese-font-size: 25px;
  --amount-chinese-color: #333;
  --amount-chinese-font-family: "KaiTi", "楷体", serif;
}

.theme-solemn {
  --primary-color: #374151;
  --primary-color-hover: #1f2937;
  --primary-text-color: #111827;
  --primary-bg-light: #f3f4f6;
  --primary-border-color: #d1d5db;
  --primary-ring-color: #4b5563;
  --header-text-color: #1f2937;
  --button-bg-color: #4b5563;
  --button-bg-hover: #374151;
  --button-border-color: #4b5563;
  --button-text-color: #4b5563;
  --link-hover-bg: #e5e7eb;
  --total-text-color: #374151;
  --type-color: #374151;
}

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

html {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Inter", "Microsoft YaHei", sans-serif;
  background: #f3f4f6;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  margin: 0;
}

.hidden {
  display: none !important;
}

#viewport-wrapper {
  width: 100%;
  max-width: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

#app-container {
  width: 100%;
  max-width: 1280px;
  padding: clamp(0.25rem, 1vh, 0.75rem);
  transform-origin: top center;
  flex-shrink: 0;
  box-sizing: border-box;
}

@media (min-width: 1024px) {
  #app-container:has(#main-screen:not(.hidden)) {
    height: 100dvh;
    max-height: 100dvh;
  }

  #main-screen:not(.hidden) {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
    min-height: 0;
  }
}

#setup-screen:not(.hidden) {
  max-height: calc(100dvh - 1rem);
  overflow: hidden;
}

/* ==================== 通用组件 ==================== */
.card {
  background: white;
  border-radius: 0.5rem;
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
  padding: clamp(0.75rem, 2vh, 1.5rem);
}



.card-narrow {
  max-width: 42rem;
  margin: 0 auto;
}

h1.title {
  font-size: clamp(1.25rem, 3vh, 1.875rem);
  font-weight: 700;
  text-align: center;
  color: var(--header-text-color);
  margin-bottom: clamp(0.5rem, 1.5vh, 1rem);
}

h2.section-title {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #e5e7eb;
}

.form-group {
  margin-bottom: clamp(0.375rem, 1vh, 0.75rem);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

label.field-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: #374151;
  margin-bottom: 0.25rem;
}

input[type="text"],
input[type="number"],
input[type="password"],
input[type="date"],
input[type="time"],
select,
textarea {
  width: 100%;
  padding: clamp(0.375rem, 1vh, 0.625rem) clamp(0.5rem, 1vh, 0.75rem);
  border: 1px solid #d1d5db;
  border-radius: 0.5rem;
  font-size: clamp(0.8125rem, 1.6vh, 1rem);
  outline: none;
  background: #fff;
  color: #111827;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary-ring-color);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-ring-color) 25%, transparent);
}

.password-field {
  position: relative;
  display: flex;
  align-items: center;
}

.password-field input {
  padding-right: 4.5rem;
}

.password-toggle {
  position: absolute;
  right: 0.375rem;
  top: 50%;
  transform: translateY(-50%);
  padding: 0.375rem 0.625rem;
  border: none;
  border-radius: 0.375rem;
  background: var(--primary-bg-light);
  color: var(--button-text-color);
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s;
}

.password-toggle:hover {
  background: var(--link-hover-bg);
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: clamp(0.375rem, 1vh, 0.625rem) clamp(0.75rem, 1.5vh, 1rem);
  border-radius: 0.5rem;
  font-size: clamp(0.8125rem, 1.6vh, 1rem);
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background 0.2s;
}

.btn-primary {
  background: var(--button-bg-color);
  color: white;
}

.btn-primary:hover {
  background: var(--button-bg-hover);
}

.btn-primary:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}

.btn-secondary {
  background: white;
  color: var(--button-text-color);
  border: 1px solid var(--button-border-color);
}

.btn-secondary:hover {
  background: var(--link-hover-bg);
}

.btn-danger {
  background: white;
  color: #dc2626;
  border: 1px solid #dc2626;
}

.btn-danger:hover {
  background: #fee2e2;
}

.btn-block {
  width: 100%;
}

.btn-lg {
  padding: clamp(0.5rem, 1.2vh, 0.75rem);
  font-size: clamp(0.875rem, 1.8vh, 1.0625rem);
}

.flex-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.flex-row select {
  flex: 1;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.radio-group label {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  cursor: pointer;
  font-size: 0.875rem;
}

.radio-group input[type="radio"] {
  accent-color: var(--button-bg-color);
  width: auto;
}

details summary {
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  color: #4b5563;
  list-style: none;
}

details summary::-webkit-details-marker {
  display: none;
}

details .details-body {
  margin-top: 1rem;
  padding: 1rem;
  background: #f9fafb;
  border-radius: 0.5rem;
  border: 1px solid #e5e7eb;
}

.hint {
  font-size: 0.75rem;
  color: #6b7280;
  margin-top: 0.5rem;
}

.themed-text {
  color: var(--total-text-color);
}

.alert {
  padding: clamp(0.375rem, 0.8vh, 0.625rem) clamp(0.5rem, 1vh, 0.75rem);
  border-radius: 0.5rem;
  margin-bottom: clamp(0.25rem, 0.6vh, 0.5rem);
  font-size: clamp(0.75rem, 1.4vh, 0.875rem);
}

.alert-warning {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fcd34d;
}

.alert-error {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fca5a5;
}

/* ==================== 主界面布局 ==================== */
.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: clamp(0.25rem, 0.8vh, 0.5rem);
  flex-shrink: 0;
  gap: 0.5rem;
}

.main-header h1 {
  font-size: clamp(1rem, 2.5vh, 1.5rem);
  font-weight: 700;
  color: var(--primary-text-color);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rotate-hint {
  display: none;
  margin: 0;
  text-align: center;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--primary-text-color);
}

.main-grid {
  display: none;
  gap: clamp(0.375rem, 1vh, 0.75rem);
}

@media (min-width: 1024px) {
  .main-grid {
    display: grid;
    grid-template-columns: minmax(260px, 1fr) minmax(0, 2fr);
    grid-template-rows: minmax(0, 1fr);
    flex: 1;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    align-items: stretch;
  }
}

@media (max-width: 1023px) {
  #app-container {
    padding: 0.25rem;
  }

  #app-container:has(#main-screen:not(.hidden)) {
    height: 100dvh;
    max-height: 100dvh;
  }

  #main-screen:not(.hidden) {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  #main-screen:not(.hidden) .rotate-hint {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    padding: 1rem;
  }

  #main-screen:not(.hidden) .main-grid {
    display: none !important;
  }

  #setup-screen:not(.hidden) {
    max-height: calc(100dvh - 0.5rem);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #select-event-section .flex-row {
    flex-wrap: wrap;
  }

  #select-event-section .flex-row select {
    width: 100%;
    flex: none;
  }

  #select-event-section .flex-row .btn {
    flex: 1;
    min-width: 0;
  }

  .modal {
    max-width: calc(100% - 1.5rem);
    margin: 0 0.75rem;
    padding: 1rem;
  }

  .modal-actions {
    flex-wrap: wrap;
  }

  .modal-actions .btn {
    flex: 1;
    min-width: 0;
  }
}

@media (min-width: 1024px) {
  .main-grid > .entry-panel,
  .main-grid > .gift-book-frame {
    min-height: 0;
    min-width: 0;
    height: 100%;
  }

  .main-grid > .entry-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .main-grid > .entry-panel .entry-panel-body {
    flex: none;
  }

  .main-grid > .entry-panel form,
  .main-grid > .entry-panel #add-gift-form,
  .main-grid > .entry-panel .func-area {
    flex: none;
    overflow: visible;
  }

  .main-grid > .gift-book-frame {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    align-self: stretch;
  }

  .gift-book-inner {
    min-height: 0;
    height: 100%;
    overflow: hidden;
  }
}

.entry-panel {
  min-width: 0;
}

@media (min-width: 1024px) {
  .entry-panel {
    align-self: stretch;
  }
}

.entry-panel.card {
  padding: clamp(0.5rem, 1.5vh, 1rem);
}

.entry-panel-body {
  display: block;
  width: 100%;
}

.entry-panel form,
#add-gift-form {
  display: block;
  overflow: visible;
}

.entry-panel .func-area {
  overflow: visible;
}

@media (max-height: 750px) {
  .entry-panel h2 {
    margin-bottom: 0.25rem;
    padding-bottom: 0.25rem;
    font-size: 1rem;
  }

  .entry-panel .form-group {
    margin-bottom: 0.375rem;
  }

  .entry-panel .func-area {
    margin-top: 0.375rem;
    padding-top: 0.375rem;
  }

  .entry-panel .amount-preview {
    min-height: 2rem;
    padding: 0.375rem;
  }

  .entry-panel .btn-lg {
    padding-top: 0.625rem;
    padding-bottom: 0.625rem;
  }
}

.entry-panel h2 {
  font-size: clamp(1rem, 2.2vh, 1.375rem);
  font-weight: 700;
  text-align: center;
  margin-bottom: clamp(0.25rem, 0.8vh, 0.5rem);
  padding-bottom: clamp(0.25rem, 0.5vh, 0.375rem);
  border-bottom: 1px solid #e5e7eb;
}

.func-area {
  margin-top: clamp(0.375rem, 1vh, 0.75rem);
  padding-top: clamp(0.375rem, 1vh, 0.75rem);
  border-top: 1px solid #e5e7eb;
}

.func-area h3 {
  font-size: clamp(0.875rem, 1.8vh, 1.0625rem);
  font-weight: 600;
  margin-bottom: clamp(0.25rem, 0.6vh, 0.5rem);
}

.func-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: clamp(0.25rem, 0.6vh, 0.5rem);
  margin-bottom: clamp(0.25rem, 0.6vh, 0.5rem);
}

.func-btns .btn {
  width: 100%;
  margin: 0;
}

.func-btns .btn:first-child {
  grid-column: 1 / -1;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: clamp(0.25rem, 0.6vh, 0.5rem);
}

.search-row input {
  flex: 1;
  min-width: 0;
  margin: 0;
}

.search-row .btn {
  width: auto;
  flex-shrink: 0;
  margin: 0;
  padding: 0.75rem 1.25rem;
  white-space: nowrap;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f3f4f6;
  padding: clamp(0.375rem, 0.8vh, 0.625rem) clamp(0.5rem, 1vh, 0.75rem);
  border-radius: 0.5rem;
  margin-top: 0;
}

.toggle-label {
  font-weight: 500;
  color: #374151;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 2.75rem;
  height: 1.5rem;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: #d1d5db;
  border-radius: 9999px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  width: 1.25rem;
  height: 1.25rem;
  left: 0.125rem;
  top: 0.125rem;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--button-bg-color);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(1.25rem);
}

.modal-close {
  float: right;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
}

.amount-preview {
  padding: clamp(0.375rem, 0.8vh, 0.625rem);
  background: var(--primary-bg-light);
  border: 1px solid var(--primary-border-color);
  border-radius: 0.5rem;
  text-align: center;
  min-height: clamp(2rem, 5vh, 3rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
}

.amount-preview-chinese {
  font-family: "KaiTi", "楷体", serif;
  font-size: 1.25rem;
  font-weight: bold;
  color: #333;
  letter-spacing: 1.5px;
}

.amount-preview-numeric {
  font-size: 0.875rem;
  color: #555;
}

.amount-preview-empty {
  color: #9ca3af;
  font-size: 0.875rem;
}

/* ==================== 礼簿样式 (屏幕显示) ==================== */
.gift-book-frame {
  border: clamp(2px, 0.4vh, 4px) solid var(--primary-color);
  padding: clamp(6px, 1.2vh, 16px);
  border-radius: 10px;
  background-color: var(--primary-bg-light);
  position: relative;
}

.gift-book-inner {
  display: flex;
  flex-direction: column;
}

.book-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: clamp(0.25rem, 0.8vh, 0.5rem);
  flex-wrap: wrap;
  gap: 0.25rem;
  flex-shrink: 0;
}

.book-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.book-stats span.label {
  font-weight: 700;
}

.book-stats span.value {
  font-size: clamp(0.875rem, 1.8vh, 1.125rem);
  font-weight: 700;
  color: var(--total-text-color);
}

.book-stats span.label {
  font-size: clamp(0.75rem, 1.4vh, 0.875rem);
}

.page-nav {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: clamp(0.8125rem, 1.6vh, 1rem);
}

.page-nav button {
  width: clamp(1.5rem, 3vh, 2rem);
  height: clamp(1.5rem, 3vh, 2rem);
  border-radius: 50%;
  border: none;
  background: var(--button-bg-color);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.page-nav button:disabled {
  background: #d1d5db;
  cursor: not-allowed;
}

#gift-book-content {
  display: grid;
  grid-template-rows: 3fr 1fr 3fr;
  border-top: 2px solid var(--primary-border-color);
  min-height: 0;
  flex: 1;
  overflow: hidden;
  transform-origin: top center;
}

.gift-book-row {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
}

.gift-book-row:nth-child(1),
.gift-book-row:nth-child(3) {
  min-height: 0;
}

.gift-book-row > .book-cell {
  min-width: 0;
  position: relative;
  line-height: 1.2;
}

.book-cell {
  border-right: 2px solid var(--primary-border-color);
  border-bottom: 2px solid var(--primary-border-color);
  display: grid;
  place-items: center;
  writing-mode: vertical-lr;
  text-orientation: mixed;
  font-weight: bold;
  font-size: clamp(10px, 1.4vh, 22px);
  letter-spacing: clamp(2px, 0.5vh, 7px);
  font-family: "KaiTi", "楷体", serif;
  padding: clamp(2px, 0.6vh, 8px) 0;
  overflow: hidden;
  text-align: center;
}

.book-cell:first-child {
  border-left: 2px solid var(--primary-border-color);
}

.name-cell .name {
  color: var(--name-color);
  padding: clamp(2px, 0.8vh, 10px) 0;
  text-align: center;
  white-space: nowrap;
  min-height: 0;
  font-size: clamp(12px, 1.8vh, var(--name-font-size));
  font-family: var(--name-font-family);
}

.name-cell .name.scale {
  font-size: clamp(14px, 1.2vw, 20px);
}

.type-cell {
  color: var(--type-color);
  font-size: clamp(11px, 1.6vh, var(--type-font-size));
  padding: clamp(2px, 0.6vh, 8px) 2px;
  font-family: var(--type-font-family);
  font-weight: bold;
}

.amount-cell {
  color: #333;
  grid-template-columns: 1fr auto;
  column-gap: clamp(8px, 1.5vw, 18px);
  justify-content: center;
}

.amount-chinese {
  letter-spacing: clamp(0.5px, 0.2vh, 1.5px);
  text-align: center;
  padding: clamp(2px, 0.8vh, 10px) 0;
  flex: 1;
  overflow: hidden;
  font-size: clamp(11px, 1.8vh, var(--amount-chinese-font-size));
  color: var(--amount-chinese-color);
  font-family: var(--amount-chinese-font-family);
}

.amount-chinese.scale {
  font-size: clamp(12px, 1.2vw, 18px);
  letter-spacing: 0.5px;
}

.amount-numeric {
  color: #555;
  letter-spacing: 1px;
  writing-mode: horizontal-tb;
  line-height: 1.5;
  font-size: clamp(8px, 1.2vh, 13px);
}

.amount-numeric.scale {
  font-size: clamp(8px, 1vw, 12px);
}

/* ==================== 模态框 ==================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal {
  background: white;
  padding: 1.5rem;
  border-radius: 0.5rem;
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25);
  width: 100%;
  max-width: 28rem;
  max-height: 80vh;
  overflow-y: auto;
}

.modal h3 {
  font-size: 1.25rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 1rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.search-result-item {
  padding: 0.75rem;
  border-bottom: 1px solid #e5e7eb;
  cursor: pointer;
}

.search-result-item:hover {
  background: var(--link-hover-bg);
}

.search-result-item:last-child {
  border-bottom: none;
}

.modal.modal-wide {
  max-width: 36rem;
}

.delete-list-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 0.5rem;
  margin-bottom: 0.5rem;
  font-size: 0.875rem;
}

.delete-list-toolbar label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-weight: 500;
}

.delete-list {
  max-height: min(50vh, 360px);
  overflow-y: auto;
  border: 1px solid #e5e7eb;
  border-radius: 0.5rem;
}

.delete-list-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid #e5e7eb;
  cursor: pointer;
  font-size: 0.875rem;
}

.delete-list-item:last-child {
  border-bottom: none;
}

.delete-list-item:hover {
  background: var(--link-hover-bg);
}

.delete-list-item input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
  accent-color: var(--button-bg-color);
  cursor: pointer;
}

.delete-list-item .item-info {
  flex: 1;
  min-width: 0;
}

.delete-list-item .item-name {
  font-weight: 600;
}

.delete-list-item .item-meta {
  color: #6b7280;
  font-size: 0.8125rem;
  margin-top: 0.125rem;
}

.delete-list-empty {
  padding: 2rem;
  text-align: center;
  color: #9ca3af;
  font-size: 0.875rem;
}

/* ==================== 打印 ==================== */
@media print {
  body {
    background: white;
  }

  .entry-panel,
  .main-header,
  .rotate-hint,
  .page-nav,
  .func-area,
  #setup-screen {
    display: none !important;
  }

  .main-grid {
    grid-template-columns: 1fr;
  }

  .gift-book-frame {
    border: 2px solid #000;
    break-inside: avoid;
  }
}
