/*
 * POAS UI refresh — shared visual and mobile usability layer.
 * Loaded after the existing module styles so functionality and selectors remain intact.
 */

:root {
  --bg: #f7f3ed;
  --card: #fffdf9;
  --border: #e5ddd2;
  --text: #302a26;
  --text2: #746b64;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(63, 45, 31, .04), 0 10px 30px rgba(63, 45, 31, .045);
  --surface-soft: rgba(var(--accent-rgb), .07);
  --surface-float: rgba(255, 253, 249, .88);
}

[data-theme="dark"] {
  --bg: #181714;
  --card: #211f1c;
  --border: #39342e;
  --text: #eee8df;
  --text2: #aaa198;
  --shadow: 0 1px 2px rgba(0, 0, 0, .16), 0 14px 36px rgba(0, 0, 0, .18);
  --surface-float: rgba(33, 31, 28, .9);
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 8% 2%, rgba(var(--accent-rgb), .055), transparent 30rem),
    var(--bg);
  letter-spacing: .005em;
}

button,
[onclick] {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid rgba(var(--accent-rgb), .24);
  outline-offset: 2px;
}

/* Layout */
.sidebar {
  width: 248px;
  padding: 30px 12px 18px;
  border-right: 1px solid var(--sidebar-divider);
}

.sidebar h1 {
  padding: 0 14px 22px;
  font-size: 22px;
  letter-spacing: 2px;
}

.sidebar nav {
  padding: 14px 0;
}

.sidebar nav a {
  min-height: 46px;
  padding: 11px 14px;
  margin: 2px 0;
  border-left: 0;
  border-radius: 12px;
  font-size: 14px;
}

.sidebar nav a.active {
  border-left-color: transparent;
  background: var(--sidebar-active);
  box-shadow: inset 3px 0 0 var(--accent-sb);
}

.main {
  margin-left: 248px;
  max-width: 1120px;
  padding: 42px 48px 56px;
}

.main.main-full {
  padding: 24px 28px;
}

#page-gate-settings,
#page-sage-settings,
#page-settings {
  max-width: 780px;
  margin-inline: auto;
}

.page-title {
  margin-bottom: 24px;
  font-size: clamp(24px, 3vw, 30px);
  line-height: 1.25;
}

/* 西语堂 page structure */
.west-page-header {
  position: sticky;
  top: 0;
  z-index: 10;
  flex-shrink: 0;
  margin: -10px 0 14px;
  padding: 10px 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(12px);
}

.west-page-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.west-page-title-row .page-title,
.west-page-header > .page-title {
  margin-bottom: 4px;
}

.west-page-subtitle {
  margin-bottom: 12px;
  color: var(--text2);
  font-size: 13px;
  line-height: 1.5;
}

.west-page-title-row .west-page-subtitle {
  margin-bottom: 0;
}

.west-page-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}

.west-refresh-btn:disabled {
  cursor: wait;
}

.west-state {
  display: flex;
  min-height: 148px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  text-align: center;
}

.west-state-icon {
  color: var(--accent);
  font-family: var(--font-serif);
  font-size: 24px;
  line-height: 1;
}

.west-state-loading .west-state-icon {
  animation: westStatePulse 1.1s ease-in-out infinite;
  letter-spacing: 3px;
}

.west-state-title {
  color: var(--text);
  font-weight: 650;
}

.west-state-message {
  max-width: 360px;
  margin-bottom: 4px;
  color: var(--text2);
  font-size: 13px;
  line-height: 1.55;
}

/* West Hall vocabulary workspace */
.vocab-page-header {
  flex: 0 0 auto;
}

.vocab-filter-stack {
  display: flex;
  align-items: center;
  gap: 8px;
}

.vocab-filter-stack .tabs {
  margin-bottom: 10px;
}

.vocab-search {
  margin-bottom: 10px;
}

.vocab-lesson-tools {
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-float);
}

.vocab-lesson-tools summary {
  display: flex;
  min-height: 42px;
  padding: 8px 12px;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
  list-style: none;
}

.vocab-lesson-tools summary::-webkit-details-marker {
  display: none;
}

.vocab-lesson-tools summary::after {
  content: "⌄";
  margin-left: auto;
  color: var(--text2);
  transition: transform .2s ease;
}

.vocab-lesson-tools[open] summary::after {
  transform: rotate(180deg);
}

.vocab-lesson-summary-hint {
  color: var(--text2);
  font-size: 12px;
  font-weight: 400;
}

.vocab-lesson-controls {
  display: flex;
  padding: 0 12px 12px;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.vocab-lesson-controls label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text2);
  font-size: 13px;
}

.vocab-lesson-controls input {
  width: 60px;
  min-height: 36px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.vocab-stats {
  min-height: 22px;
  color: var(--text2);
  font-size: 12px;
  line-height: 1.5;
}

.vocab-scroll-region {
  flex: 1 1 auto;
  min-height: 180px;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.vocab-scroll-region #customLessonStatus {
  margin-bottom: 8px;
}

#vocabList {
  min-height: 100%;
  margin-bottom: 0;
}

#vocabList table {
  min-width: 680px;
  table-layout: fixed;
}

#vocabList th:nth-child(1) {
  width: 40px;
}

#vocabList th:nth-child(2) {
  width: 150px;
}

#vocabList th:nth-child(3) {
  width: 245px;
}

#vocabList th:nth-child(4) {
  width: 62px;
}

#vocabList th:nth-child(5) {
  width: 82px;
}

#vocabList th:nth-child(6) {
  width: 132px;
}

#vocabList td:nth-child(2),
#vocabList td:nth-child(3) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap !important;
}

#vocabList thead {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--card);
}

.management-title {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.lesson-custom-badge {
  margin-left: 4px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 11px;
}

.mobile-row-meta,
.mobile-action-label {
  display: none;
}

.management-actions {
  white-space: nowrap !important;
}

.management-actions .btn + .btn {
  margin-left: 4px;
}

.article-mobile-sort {
  display: none;
}

@keyframes westStatePulse {
  0%, 100% { opacity: .35; }
  50% { opacity: 1; }
}

/* Surfaces and controls */
.card,
.stat-card,
.review-card,
.lesson-entry,
.writing-history-item,
.error-record-item {
  border-color: var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card {
  padding: 24px;
}

.card h3 {
  font-size: 16px;
  line-height: 1.45;
}

.stats-grid {
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card {
  padding: 20px 16px;
  text-align: left;
}

.stat-card .num {
  font-size: 28px;
  line-height: 1.15;
}

.stat-card .label {
  margin-top: 8px;
  font-size: 12px;
  text-transform: none;
  letter-spacing: .04em;
}

.btn {
  min-height: 42px;
  padding: 10px 16px;
  justify-content: center;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.2;
}

.btn-primary {
  box-shadow: 0 7px 18px rgba(var(--accent-rgb), .16);
}

.btn-primary:hover {
  filter: brightness(.94);
  transform: none;
  box-shadow: 0 8px 22px rgba(var(--accent-rgb), .22);
}

.btn-secondary {
  background: var(--surface-float);
}

.btn-sm {
  min-height: 36px;
  padding: 8px 12px;
  font-size: 13px;
}

input,
textarea,
select,
.search-bar {
  min-height: 44px;
  border-radius: 12px !important;
}

textarea {
  padding: 12px 14px !important;
}

input[type="checkbox"],
input[type="radio"] {
  min-height: auto;
  accent-color: var(--accent);
}

.form-group label {
  margin-bottom: 7px;
  color: var(--text);
}

.tabs {
  width: fit-content;
  max-width: 100%;
  padding: 4px;
  gap: 3px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(var(--accent-rgb), .045);
}

.tab {
  min-height: 36px;
  margin-bottom: 0;
  padding: 7px 14px;
  border: 0;
  border-radius: 10px;
}

.tab.active {
  border-bottom-color: transparent;
  color: var(--accent);
  background: var(--card);
  box-shadow: 0 1px 5px rgba(39, 30, 22, .08);
}

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

.palette-swatch {
  min-width: 0;
  min-height: 112px;
  padding: 14px 10px;
  border-radius: 14px;
}

.empty {
  padding: 36px 20px;
  border-radius: 14px;
  color: var(--text2);
  background: rgba(var(--accent-rgb), .035);
}

.search-bar {
  padding-block: 10px;
  background: var(--card);
}

.progress-bar {
  height: 6px;
  border-radius: 999px;
}

/* Entry and login */
.login-overlay {
  padding:
    max(20px, env(safe-area-inset-top))
    20px
    max(20px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 18% 10%, rgba(197, 148, 99, .2), transparent 30rem),
    linear-gradient(150deg, #27231f, #37302a 55%, #211e1b);
}

.login-card {
  width: 400px;
  padding: 44px 38px;
  border: 1px solid rgba(255, 255, 255, .35);
  border-radius: 28px;
}

.login-card h2 {
  margin-bottom: 24px;
  letter-spacing: 5px;
}

.login-card input {
  min-height: 50px;
  font-size: 16px;
}

.gate-container {
  min-height: calc(100dvh - 48px);
  max-width: 980px;
  margin-inline: auto;
  padding: 54px 18px 42px;
}

.gate-header {
  font-size: clamp(44px, 7vw, 64px);
  line-height: 1;
  letter-spacing: .14em;
}

.gate-sub {
  margin-bottom: 44px;
  letter-spacing: .04em;
}

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

.gate-card {
  width: 100%;
  min-width: 0;
  max-width: none;
  padding: 26px 22px 22px;
  gap: 10px;
  color: var(--text);
  font: inherit;
  text-align: center;
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.gate-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 36px rgba(61, 46, 34, .09);
}

.gate-icon {
  display: grid;
  width: 64px;
  height: 64px;
  margin-bottom: 4px;
  place-items: center;
  border-radius: 20px;
  background: var(--accent-light);
  font-size: 32px;
}

.gate-title {
  font-size: 22px;
}

.gate-card .btn {
  margin-top: 10px;
}

.gate-settings-link {
  display: inline-flex;
  min-height: 42px;
  padding: 9px 14px;
  align-items: center;
  color: var(--text2);
  font-size: 13px;
  text-decoration: none;
  border-radius: 12px;
}

.gate-settings-link:hover {
  color: var(--text);
  background: var(--card);
}

/* Reading and study */
.article-reader .content {
  font-size: 17px;
  line-height: 1.95;
}

.article-reader .content p {
  margin-bottom: 1.25em;
}

.flashcard {
  border-width: 1px;
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.modal-overlay {
  padding: 18px;
  backdrop-filter: blur(5px);
}

.modal {
  border: 1px solid var(--border);
  border-radius: 22px;
}

.toast {
  border: 1px solid rgba(255, 255, 255, .16);
  border-radius: 14px;
}

@media (hover: none) {
  .btn:hover,
  .gate-card:hover,
  .review-card:hover,
  .lesson-entry:hover {
    transform: none;
  }
}

@media (max-width: 768px) {
  .sidebar-arrow {
    top: max(12px, env(safe-area-inset-top));
    left: 12px;
    width: 44px;
    height: 44px;
    transform: none;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface-float);
    color: var(--text);
    font-size: 19px;
    line-height: 1;
    box-shadow: var(--shadow);
    backdrop-filter: blur(14px);
  }

  .sidebar-arrow.open {
    left: min(calc(86vw - 56px), 248px);
    transform: none;
    border-radius: 14px;
    color: var(--sidebar-text);
    background: var(--sidebar-active);
  }

  .sidebar {
    width: min(86vw, 304px);
    padding-top: max(28px, env(safe-area-inset-top));
    transform: translateX(-105%);
    box-shadow: 18px 0 50px rgba(20, 16, 12, .2);
  }

  .sidebar nav a {
    min-height: 48px;
    font-size: 15px;
  }

  .sidebar-overlay {
    background: rgba(28, 23, 19, .42);
    backdrop-filter: blur(3px);
  }

  .main {
    margin-left: 0;
    padding:
      calc(68px + env(safe-area-inset-top))
      14px
      calc(16px + env(safe-area-inset-bottom));
    overflow-x: hidden;
  }

  .main.main-full {
    padding:
      max(12px, env(safe-area-inset-top))
      12px
      max(12px, env(safe-area-inset-bottom));
  }

  body.in-sage .main.main-full,
  body.in-agent .main.main-full,
  body.in-soulchat .main.main-full {
    padding:
      max(12px, env(safe-area-inset-top))
      12px
      max(12px, env(safe-area-inset-bottom));
  }

  body.in-wenjianshi .main.main-full {
    padding: 0;
  }

  .page {
    overflow-x: hidden;
  }

  #page-vocab.active,
  #page-writing.active,
  #page-exercises.active {
    height: calc(100dvh - 84px);
  }

  #page-articles.active,
  #page-lessons.active,
  #page-review.active,
  #page-generate.active {
    max-height: calc(100dvh - 84px);
  }

  .page-title {
    font-size: 23px;
    margin-bottom: 18px;
  }

  .west-page-header {
    margin: -6px 0 10px;
    padding: 6px 0 10px;
  }

  .west-page-title-row {
    flex-wrap: wrap;
    margin-bottom: 12px;
  }

  .west-page-title-row .page-title,
  .west-page-header > .page-title {
    margin-bottom: 3px;
  }

  .west-page-subtitle {
    margin-bottom: 9px;
    font-size: 12px;
  }

  .vocab-page-header {
    margin-bottom: 6px;
    padding-bottom: 6px;
  }

  .vocab-page-header .west-page-title-row {
    margin-bottom: 7px;
  }

  .vocab-page-header .west-page-subtitle {
    display: none;
  }

  .vocab-filter-stack {
    gap: 6px;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .vocab-filter-stack::-webkit-scrollbar {
    display: none;
  }

  .vocab-filter-stack .tabs {
    width: max-content;
    max-width: none;
    margin-bottom: 7px;
    flex: 0 0 auto;
  }

  .vocab-filter-stack .tab {
    min-height: 34px;
    padding: 6px 11px;
  }

  .vocab-search {
    margin-bottom: 7px;
  }

  .vocab-search .search-bar {
    min-height: 40px;
  }

  .vocab-lesson-tools {
    margin-bottom: 5px;
  }

  .vocab-lesson-tools summary {
    min-height: 38px;
    padding: 6px 10px;
  }

  .vocab-lesson-summary-hint {
    display: none;
  }

  .vocab-lesson-controls {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    padding: 0 8px 8px;
  }

  .vocab-lesson-controls .btn {
    width: 100%;
  }

  .vocab-lesson-controls #customLessonSelBtn {
    grid-column: 1 / -1;
  }

  .vocab-stats {
    min-height: 18px;
    white-space: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .vocab-stats::-webkit-scrollbar {
    display: none;
  }

  .vocab-scroll-region {
    min-height: 220px;
  }

  #vocabList {
    padding: 0;
    overflow: visible;
  }

  #vocabList th,
  #vocabList td {
    padding-block: 5px;
  }

  #vocabList select {
    min-height: 34px;
    padding: 4px 6px;
    font-size: 13px !important;
  }

  .management-table-wrap {
    padding: 0;
    overflow-x: hidden !important;
  }

  #articlesList table.management-table,
  #lessonsList table.management-table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  .management-table th,
  .management-table td {
    padding: 6px 4px;
    vertical-align: middle;
  }

  .management-table .desktop-detail-cell {
    display: none;
  }

  .management-table .management-select-cell {
    width: 34px;
    text-align: center;
  }

  .management-table .management-status-cell {
    width: 57px;
    text-align: center;
  }

  .management-table .management-status-cell .badge {
    padding-inline: 6px;
    white-space: nowrap;
  }

  .management-table .article-management-actions {
    width: 88px;
  }

  .management-table .lesson-management-actions {
    width: 84px;
  }

  .management-title-cell {
    min-width: 0;
  }

  .management-title {
    display: -webkit-box;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    line-height: 1.35;
  }

  .mobile-row-meta {
    display: block;
    margin-top: 3px;
    overflow: hidden;
    color: var(--text2);
    font-size: 11px;
    line-height: 1.3;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .lesson-custom-badge {
    display: none;
  }

  .desktop-action-label,
  .mobile-hide-action {
    display: none;
  }

  .mobile-action-label {
    display: inline;
  }

  .management-actions .btn {
    min-height: 32px;
    padding: 5px 7px;
    font-size: 12px;
  }

  .article-mobile-sort {
    display: grid;
    grid-template-columns: auto repeat(2, minmax(0, 1fr));
    margin-top: -7px;
    align-items: center;
    gap: 6px;
  }

  .article-mobile-sort > span {
    color: var(--text2);
    font-size: 11px;
    white-space: nowrap;
  }

  .article-mobile-sort .btn {
    min-width: 0;
    min-height: 38px;
    padding: 6px;
    justify-content: center;
    white-space: nowrap;
  }

  .article-mobile-sort .btn.active {
    border-color: rgba(var(--accent-rgb), .32);
    background: var(--accent-light);
    color: var(--accent);
  }

  .west-page-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .west-page-actions .btn {
    min-width: 0;
    padding-inline: 8px;
  }

  .west-state {
    min-height: 132px;
    padding: 20px 14px;
  }

  .card {
    padding: 18px;
    margin-bottom: 12px;
  }

  .btn {
    min-height: 44px;
  }

  .btn-sm {
    min-height: 40px;
    padding: 9px 12px;
  }

  input,
  textarea,
  select,
  .search-bar {
    font-size: 16px !important;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .stat-card {
    min-height: 94px;
    padding: 16px;
  }

  .tabs {
    width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .tabs::-webkit-scrollbar {
    display: none;
  }

  .tab {
    min-height: 40px;
    flex: 0 0 auto;
  }

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

  .palette-swatch {
    min-height: 100px;
    padding: 12px 6px;
  }

  .palette-en {
    font-size: 10px;
  }

  .gate-container {
    min-height: auto;
    padding: 44px 2px 28px;
  }

  .gate-header {
    font-size: 42px;
  }

  .gate-sub {
    max-width: 300px;
    margin: 9px auto 32px;
    font-size: 12px;
    line-height: 1.75;
  }

  .gate-cards {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .gate-card {
    display: grid;
    grid-template-columns: 56px minmax(0, 1fr) 44px;
    grid-template-rows: auto auto;
    padding: 16px;
    gap: 2px 14px;
    align-items: center;
    text-align: left;
  }

  .gate-icon {
    grid-row: 1 / 3;
    width: 56px;
    height: 56px;
    margin: 0;
    border-radius: 17px;
    font-size: 28px;
  }

  .gate-title {
    align-self: end;
    font-size: 19px;
  }

  .gate-desc {
    align-self: start;
    font-size: 12px;
  }

  .gate-card .btn {
    grid-column: 3;
    grid-row: 1 / 3;
    width: 44px !important;
    min-width: 44px;
    height: 44px;
    margin: 0;
    padding: 0;
    border-radius: 50%;
    font-size: 0;
  }

  .gate-card .btn::after {
    content: "→";
    font-size: 18px;
  }

  .login-card {
    width: min(100%, 380px);
    padding: 38px 24px 30px;
    border-radius: 24px;
  }

  .modal-overlay {
    padding: 10px;
    align-items: flex-end;
  }

  .modal {
    width: 100%;
    max-height: 88dvh;
    padding: 22px 18px max(18px, env(safe-area-inset-bottom));
    border-radius: 24px 24px 14px 14px;
    overflow-y: auto;
  }

  .toast-container {
    top: auto;
    right: 12px;
    bottom: calc(18px + env(safe-area-inset-bottom));
    left: 12px;
  }

  .toast {
    width: 100%;
    max-width: none;
    transform: translateY(140%);
  }

  .toast.show {
    transform: translateY(0);
  }

  .toast.hide {
    transform: translateY(140%);
  }

  .article-reader .content {
    font-size: 16.5px;
    line-height: 1.95;
  }

  .flashcard {
    min-height: 210px;
    margin: 16px 0;
  }

  #page-articles > div:first-child > div:last-child .btn {
    flex: 1;
  }
}

@media (max-width: 380px) {
  .palette-grid {
    grid-template-columns: 1fr;
  }

  .palette-swatch {
    min-height: 76px;
    flex-direction: row;
    justify-content: flex-start;
    padding-inline: 14px;
  }

  .palette-en {
    margin-left: auto;
  }
}

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