/* Texta shared controls. Reference rationale lives in DESIGN.md. */
:root {
  --radius: 10px;
  --control-line: color-mix(in srgb, var(--muted) 72%, var(--line));
  --control-hover: color-mix(in srgb, var(--primary) 7%, var(--panel));
  --control-shadow: 0 2px 5px color-mix(in srgb, var(--text) 9%, transparent);
  --control-radius: 10px;
  --card-radius: 14px;
}

/* A single interaction vocabulary for native and dynamically rendered controls. */
body button, body summary {
  touch-action: manipulation;
  font-weight: 500;
  transition: background-color var(--fast), border-color var(--fast), color var(--fast), box-shadow var(--fast), transform var(--fast) var(--ease-out);
}
body button { background-image: none; box-shadow: none; }
body button:hover:not(:disabled) { background-color: var(--control-hover); border-color: var(--control-line); box-shadow: none; }
body button:active:not(:disabled) { transform: translateY(1px); box-shadow: none; }
body button:disabled { transform: none; box-shadow: none; opacity: .48; }
body button.primary-btn, body button.mastery-btn.active {
  color: var(--white); background: var(--primary); border-color: var(--primary); font-weight: 600;
}
body button.primary-btn:hover:not(:disabled), body button.mastery-btn.active:hover:not(:disabled) {
  color: var(--white); background: var(--primary-press); border-color: var(--primary-press); box-shadow: var(--control-shadow);
}
body button.text-btn, body button.ghost-btn { background: transparent; border-color: transparent; }
body button.text-btn:hover:not(:disabled), body button.ghost-btn:hover:not(:disabled) { border-color: transparent; background: var(--soft); }
body button:focus-visible, body summary:focus-visible, body a:focus-visible, body [tabindex]:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 3px;
}
body :is(input:not([type="checkbox"]), textarea, select) {
  background-color: var(--field); color: var(--text); border: 1px solid var(--control-line); border-radius: var(--control-radius);
  box-shadow: none; min-width: 0; transition: border-color var(--fast), background-color var(--fast);
}
body :is(input:not([type="checkbox"]), textarea, select):hover:not(:disabled) { border-color: var(--primary); }
body :is(input:not([type="checkbox"]), textarea, select):focus-visible { border-color: var(--primary); outline: 2px solid var(--primary); outline-offset: 3px; box-shadow: none; }
body :is(input, textarea, select)[aria-invalid="true"] { border-color: var(--warning); outline-color: var(--warning); }
body :is(input, textarea, select):disabled { cursor: not-allowed; opacity: .55; }
body :is(input, textarea)::placeholder { color: var(--muted); opacity: 1; }
body select { cursor: pointer; padding-inline-end: 32px; text-overflow: ellipsis; }
body select option { color: var(--text); background: var(--panel); }
body input[type="search"] { padding-inline-start: 40px; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%23798078' stroke-width='1.7' stroke-linecap='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cpath d='m16 16 4 4'/%3E%3C/svg%3E"); background-position: 12px center; background-repeat: no-repeat; }

/* Progressive enhancement: the native select still owns keyboard and form behavior.
   Browsers without customizable selects retain the platform picker. */
@supports (appearance: base-select) {
  body select, body select::picker(select) { appearance: base-select; }
  body select { display: flex; align-items: center; gap: 12px; padding: 10px 14px; }
  body select::picker-icon { content: ""; width: 7px; height: 7px; border: solid var(--muted); border-width: 0 1.5px 1.5px 0; rotate: 45deg; margin-block-start: -4px; transition: rotate var(--fast); }
  body select:open { border-color: var(--primary); }
  body select:open::picker-icon { rotate: 225deg; }
  body select::picker(select) { margin-block: 6px; padding: 6px; border: 0; border-radius: 12px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); max-height: min(360px,60dvh); }
  body select option { display: flex; gap: 12px; padding: 10px 12px; min-height: 44px; border-radius: 7px; white-space: normal; }
  body select option:hover, body select option:focus { background: var(--soft); outline: none; }
  body select option:checked { color: var(--primary); background: var(--soft); font-weight: 600; }
  body select option::checkmark { color: var(--primary); order: 1; margin-left: auto; }
}

body input[type="checkbox"] {
  appearance: none; display: inline-grid; place-content: center; flex-shrink: 0;
  width: 20px; height: 20px; min-height: 20px; margin: 0; padding: 0;
  border: 1px solid var(--control-line); border-radius: 6px; background: var(--field); cursor: pointer;
  transition: background-color var(--fast), border-color var(--fast);
}
body input[type="checkbox"]::before { content: ""; width: 10px; height: 6px; border-left: 2px solid var(--white); border-bottom: 2px solid var(--white); transform: translateY(-1px) rotate(-45deg) scale(0); transition: transform var(--fast) var(--ease-out); }
body input[type="checkbox"]:checked { background: var(--primary); border-color: var(--primary); }
body input[type="checkbox"]:checked::before { transform: translateY(-1px) rotate(-45deg) scale(1); }
body input[type="checkbox"]:indeterminate { background: var(--primary); border-color: var(--primary); }
body input[type="checkbox"]:indeterminate::before { border-left: 0; height: 0; transform: none; }
body .quick-row, body .switch-label { cursor: pointer; }
body .switch-label input { display: block; position: relative; width: 44px; height: 26px; min-height: 26px; border-radius: 20px; background: var(--muted); border: 1px solid transparent; }
body .switch-label input::before { content: none; }
body .switch-label input::after { top: 3px; left: 3px; width: 18px; height: 18px; background: var(--panel); box-shadow: var(--control-shadow); }
body .switch-label input:checked { background: var(--primary); }
body .switch-label input:checked::after { transform: translateX(18px); background: var(--white); }
body .switch-label:has(input:focus-visible) { color: var(--primary); }

/* Segmented controls keep the current choice legible through hover and focus. */
body :is(.auth-tabs,.notebook-layout-toggle,.calendar-size-toggle) { padding: 4px; gap: 4px; border: 0; background: var(--soft); border-radius: 12px; }
body :is(.auth-tabs,.notebook-layout-toggle,.calendar-size-toggle) > button { border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--muted); }
body :is(.auth-tabs,.notebook-layout-toggle,.calendar-size-toggle) > button:is(.active,[aria-pressed="true"]) { background: var(--panel); color: var(--primary); border-color: transparent; box-shadow: var(--control-shadow); font-weight: 600; }
body .actions button.is-active, body .theme-toggle-btn.is-active { background: var(--soft); color: var(--primary); border-color: var(--primary); }
body .theme-toggle-btn.is-active::after { content: ""; margin-left: auto; width: 8px; height: 5px; border: solid currentColor; border-width: 0 0 2px 2px; transform: rotate(-45deg); }
body .library-tab:hover:not(:disabled) { background: var(--soft); }
body .notebook-category button:hover:not(:disabled) { background: transparent; border-bottom-color: var(--primary); }

body .account-menu > summary, body .export-menu > summary { border: 1px solid var(--line); background: var(--panel); }
body details[open]:is(.account-menu,.export-menu) > summary { border-color: var(--primary); background: var(--soft); }
body summary::after { flex-shrink: 0; transition: transform var(--fast); }
body details[open] > summary::after { transform: translateY(3px) rotate(225deg); }
body :is(.account-popover,.export-popover) { padding: 16px; border-radius: var(--card-radius); max-height: calc(100dvh - 100px); overflow-y: auto; overscroll-behavior: contain; }
body .account-popover > button, body .user-row > button { width: 100%; justify-content: flex-start; }
body .export-popover > button { justify-content: flex-start; border-color: transparent; }
body .account-popover a { padding-block: 6px; }
body .usage-card { margin-top: 16px; }
body .wechat-connection { padding-block: 16px; margin-block: 16px; border-block: 1px solid var(--line); }
body .connection-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; font-size: 14px; font-weight: 600; }
body .connection-state { color: var(--muted); font-size: 13px; font-weight: 500; }
body .connection-state[data-state="bound"] { color: var(--primary); font-weight: 600; }
body .connection-state[data-state="error"] { color: var(--warning); }
body .connection-hint, body .connection-help ol { margin: 8px 0 0; color: var(--muted); font-size: 13px; line-height: 1.65; overflow-wrap: anywhere; }
body .connection-actions { display: flex; margin-top: 4px; }
body .connection-actions button { min-height: 44px; padding-inline: 0; color: var(--primary); }
body .connection-help summary { min-height: 44px; padding: 6px 0; border: 0; color: var(--primary); background: transparent; justify-content: space-between; font-size: 13px; }
body .connection-help ol { padding-left: 20px; }
body .connection-help li + li { margin-top: 8px; }
@media (max-width: 900px) {
  body .account-popover:has(.wechat-connection) { max-height: calc(100dvh - 210px); }
}
body .theme-toggle-btn { min-height: 44px; }
body :is(.fav-delete,.notebook-delete,#deleteFolderBtn,[data-action="reject"]) { color: var(--warning); }
body :is(.fav-delete,.notebook-delete,#deleteFolderBtn,[data-action="reject"]):hover:not(:disabled) { background: color-mix(in srgb,var(--warning) 9%,var(--panel)); border-color: var(--warning); }

body .fav-item { border-radius: 10px; padding-inline: 16px; transition: background-color var(--fast); }
body .notebook-entries[data-view="cards"] .glossary-item { background: var(--panel); border: 1px solid var(--line); border-radius: var(--card-radius); transition: border-color var(--fast); }
body .notebook-entries[data-view="cards"] .glossary-item:focus-within, body .notebook-entries[data-view="cards"] .glossary-item:hover { border-color: var(--control-line); }
body .glossary-panel, body .modal-card { border-radius: var(--card-radius); }
body .notebook-detail-toggle:hover:not(:disabled), body .definition-pagination button:hover:not(:disabled), body .speak-btn:hover:not(:disabled) { background: var(--panel); border-color: transparent; }
body .context-sentence { border-left-width: 1px; }
body .auth-page-wrap > .auth-card:not(.pay-card) { width: min(472px,100%); padding: 32px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); }
body .auth-card h1 { margin-bottom: 24px; }
body .auth-msg:not(:empty) { padding: 12px 14px; border-radius: 8px; background: color-mix(in srgb,var(--warning) 7%,var(--panel)); overflow-wrap: anywhere; }
body .auth-msg.success { background: var(--soft); }
body .password-toggle[aria-pressed="true"] { color: var(--primary); background: var(--soft); }
body .password-toggle { min-height: 44px; }
body .modal-head .ghost-btn { min-width: 64px; }
body .quick-row { padding: 8px 0; }

/* Busy is explicit, rather than inferred from disabled (which can mean unavailable). */
body button[aria-busy="true"], body #generateBtn.is-loading { opacity: 1; cursor: progress; }
body button[aria-busy="true"]::before, body #generateBtn.is-loading::before {
  content: ""; width: 16px; height: 16px; flex: 0 0 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 800ms linear infinite;
}
body #generationProgress { display: inline-flex; align-items: center; gap: 10px; }
body #generationProgress::before { content: ""; width: 24px; height: 3px; border-radius: 2px; background: var(--primary); animation: progress-breathe 1.4s ease-in-out infinite alternate; }
@keyframes progress-breathe { from { opacity: .35; transform: scaleX(.55); } to { opacity: 1; transform: scaleX(1); } }

/* Account/admin pages share the same primitives instead of loading legacy app CSS. */
body.admin-page, body.payment-page { min-height: 100dvh; height: auto; overflow: visible; }
body.admin-page .panel { padding: 24px; border-radius: var(--card-radius); }
body.admin-page button[data-action="approve"] { background: var(--primary); color: var(--white); border-color: var(--primary); }
body.admin-page button[data-action="approve"]:hover:not(:disabled) { background: var(--primary-press); }
.admin-list { display: grid; gap: 12px; }
.admin-item { padding: 16px; border: 1px solid var(--line); border-radius: var(--card-radius); overflow-wrap: anywhere; }
.usage-toolbar { display: grid; grid-template-columns: minmax(0,1fr) minmax(150px,200px) minmax(180px,240px); gap: 12px; margin: 24px 0 20px; align-items: end; }
.usage-toolbar label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 600; }
.usage-summary-row, .usage-detail-simple-head { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,180px),1fr)); gap: 12px; margin-bottom: 20px; }
.usage-summary-card, .usage-simple-card { padding: 16px; background: var(--soft); border-radius: var(--control-radius); }
.usage-summary-title, .usage-simple-label, .usage-count-label { color: var(--muted); font-size: 13px; }
.usage-summary-number, .usage-count-number { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; }
.usage-item { display: flex; align-items: center; justify-content: space-between; gap: 20px; flex-wrap: wrap; }
.usage-user-head { flex: 1; min-width: 0; }
.usage-meta-line { margin-top: 4px; }
.usage-right { display: flex; align-items: center; flex-wrap: wrap; gap: 12px; }
.usage-count-box { text-align: center; min-width: 100px; padding: 8px 12px; background: var(--soft); border-radius: var(--control-radius); }
.usage-detail-btn { display: inline-flex; align-items: center; min-height: 44px; padding: 8px; white-space: nowrap; }
.usage-plan-btn { min-width: 112px; }
.usage-empty { padding: 24px 0; color: var(--muted); }
.usage-detail-grid { display: grid; gap: 24px; }
.usage-detail-panel { padding-block: 20px; border-top: 1px solid var(--line); }
.usage-simple-name { margin-top: 8px; font-size: 26px; font-weight: 600; overflow-wrap: anywhere; }
.usage-simple-count { margin-top: 8px; font-size: 36px; font-weight: 650; color: var(--primary); font-variant-numeric: tabular-nums; }
.usage-detail-title { font-size: 18px; font-weight: 600; }
.usage-chart { margin-top: 20px; }
.usage-column-chart { display: grid; grid-template-columns: repeat(auto-fit,minmax(48px,64px)); gap: 12px; align-items: end; }
.usage-column-item { display: grid; gap: 8px; justify-items: center; }
.usage-column-top { font-size: 13px; font-weight: 600; color: var(--primary); }
.usage-column-track { width: 100%; height: 168px; display: flex; align-items: end; background: var(--soft); border-radius: 8px; overflow: hidden; }
.usage-column-fill { width: 100%; background: var(--primary); border-radius: 8px 8px 0 0; }
.usage-column-label { font-size: 12px; color: var(--muted); }
.usage-heatmap-grid { display: grid; grid-template-columns: repeat(6,minmax(0,1fr)); gap: 10px; }
.usage-heatmap-cell { padding: 14px; background: var(--soft); border-radius: var(--control-radius); }
.usage-heatmap-cell[data-level="1"] { background: color-mix(in srgb,var(--primary) 10%,var(--panel)); }
.usage-heatmap-cell[data-level="2"] { background: color-mix(in srgb,var(--primary) 17%,var(--panel)); }
.usage-heatmap-cell[data-level="3"] { background: color-mix(in srgb,var(--primary) 24%,var(--panel)); }
.usage-heatmap-cell[data-level="4"] { background: color-mix(in srgb,var(--primary) 31%,var(--panel)); }
.usage-heatmap-cell[data-level="5"] { background: var(--primary); color: var(--white); }
.usage-heatmap-hour { font-size: 13px; }
.usage-heatmap-count { margin-top: 6px; font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; }
#detailStatus:empty { display: none; }
.pay-box { padding: 16px; margin-block: 24px; border-radius: var(--control-radius); }
.pay-title { font-size: 14px; font-weight: 600; margin-bottom: 12px; }
.pay-qr-placeholder { display: grid; place-items: center; padding: 12px; border-radius: var(--control-radius); }
.pay-qr-placeholder img { display: block; max-width: 100%; max-height: 260px; object-fit: contain; }
.pay-tip { margin: 12px 0 0; font-size: 13px; }
.auth-subtitle { color: var(--muted); }

@media (max-width: 860px) {
  body input:not([type="checkbox"]), body select, body textarea { font-size: 16px; }
  body .speak-btn { min-height: 44px; }
  body .auth-page-wrap > .auth-card:not(.pay-card) { padding: 24px; }
  body .auth-page-wrap { padding-inline: 16px; }
  body .calendar-size-toggle { flex-wrap: wrap; }
  body .fav-actions { flex-direction: row; }
  body.admin-page .panel { padding: 16px; }
  .usage-toolbar { grid-template-columns: minmax(0,1fr); }
  .usage-right { width: 100%; }
  .usage-heatmap-grid { grid-template-columns: repeat(3,minmax(0,1fr)); }
  .usage-heatmap-cell { padding: 10px; }
  body.payment-page .pay-card { padding: 24px; }
}
@media (forced-colors: active) {
  body input[type="checkbox"] { appearance: auto; }
  body input[type="checkbox"]::before, body input[type="checkbox"]::after { content: none; }
  body .switch-label input { width: 24px; }
  body button[aria-pressed="true"], body .auth-tab.active { outline: 2px solid Highlight; }
}
@media (prefers-reduced-motion: reduce) {
  body #generationProgress::before, body button[aria-busy="true"]::before { animation: none; }
}
