:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f0f1f6;
  --border: #e2e4ec;
  --text: #1c1e2b;
  --muted: #6b6f82;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef0ff;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --success: #16a34a;
  --success-soft: #e8f7ee;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 22, 40, 0.04), 0 4px 16px rgba(20, 22, 40, 0.05);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #181a23;
    --surface-2: #20232e;
    --border: #2c303d;
    --text: #e8e9f0;
    --muted: #9a9eb2;
    --primary: #7c74ff;
    --primary-hover: #918aff;
    --primary-soft: #23224a;
    --danger: #f26363;
    --danger-soft: #3a1c20;
    --success: #4ade80;
    --success-soft: #16301f;
    --warn: #f0b35a;
    --warn-soft: #362a16;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Noto Sans Thai', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--primary);
  text-decoration: none;
}

h1,
h2 {
  line-height: 1.3;
  margin: 0;
}

h2 {
  font-size: 1.15rem;
}

input,
select,
textarea,
button {
  font: inherit;
  color: inherit;
}

input:not([type='checkbox']):not([type='radio']),
select,
textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

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

textarea {
  resize: vertical;
}

select {
  width: auto;
  cursor: pointer;
}

input[type='checkbox'],
input[type='radio'] {
  accent-color: var(--primary);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px;
}

.page {
  padding-top: 24px;
  padding-bottom: 48px;
  min-height: calc(100vh - 130px);
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.85rem;
}

.nowrap {
  white-space: nowrap;
}

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
  color: var(--text);
}

.btn:hover {
  background: var(--surface-2);
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: var(--danger);
  filter: brightness(1.08);
}

.btn-sm {
  padding: 4px 12px;
  font-size: 0.875rem;
}

.btn-lg {
  padding: 12px 24px;
  font-size: 1rem;
}

.btn-block {
  width: 100%;
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 0;
}

.icon-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 8px;
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  font-size: 1rem;
}

.icon-btn:hover {
  background: var(--surface-2);
  border-color: var(--border);
}

.icon-btn.danger:hover {
  background: var(--danger-soft);
}

.icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

/* ---------- layout ---------- */

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-inner {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 60px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  margin-right: auto;
}

.topbar-link {
  color: var(--muted);
  font-size: 0.9rem;
}

.topbar-link:hover {
  color: var(--primary);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tabs a {
  padding: 8px 14px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
}

.tabs a:hover {
  color: var(--text);
  background: var(--surface-2);
}

.tabs a.active {
  color: var(--primary);
  background: var(--primary-soft);
}

.footer {
  padding-top: 24px;
  padding-bottom: 24px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card > h2 {
  margin-bottom: 16px;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.loading {
  text-align: center;
  color: var(--muted);
  padding: 48px 0;
}

.empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.empty h2 {
  color: var(--text);
}

.empty-icon {
  font-size: 2.5rem;
}

.alert {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 0.9rem;
}

.alert-error {
  background: var(--danger-soft);
  color: var(--danger);
}

/* ---------- public ---------- */

.hero {
  text-align: center;
  padding: 32px 0 28px;
}

.hero h1 {
  font-size: clamp(1.6rem, 4vw, 2.2rem);
}

.hero p {
  color: var(--muted);
  margin: 8px auto 0;
  max-width: 560px;
}

.search {
  position: relative;
  max-width: 560px;
  margin: 24px auto 0;
}

.search input {
  padding: 12px 16px 12px 44px !important;
  border-radius: 999px !important;
  font-size: 1rem;
  box-shadow: var(--shadow);
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
}

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 12px;
}

.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.file-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}

.file-card:hover {
  border-color: var(--primary);
}

.file-card-main {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: 1;
  min-width: 0;
  color: inherit;
}

.file-icon {
  font-size: 1.5rem;
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: var(--surface-2);
  border-radius: 10px;
}

.file-icon-lg {
  font-size: 2rem;
  width: 64px;
  height: 64px;
  border-radius: 14px;
}

.file-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.file-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-desc {
  color: var(--muted);
  font-size: 0.875rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.file-page {
  max-width: 760px;
  margin: 0 auto;
}

.back {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--muted);
}

.back:hover {
  color: var(--primary);
}

.file-detail {
  padding: 28px;
}

.file-detail-head {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-bottom: 20px;
}

.file-title {
  font-size: 1.4rem;
  word-break: break-word;
}

.preview {
  display: block;
  max-width: 100%;
  max-height: 480px;
  margin: 0 auto 20px;
  border-radius: 10px;
  background: var(--surface-2);
}

.preview-audio {
  width: 100%;
  margin-bottom: 20px;
}

.file-description {
  white-space: pre-wrap;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 14px 16px;
  margin: 0 0 20px;
}

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin: 0 0 24px;
}

.facts div {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
}

.facts dt {
  color: var(--muted);
  font-size: 0.8rem;
}

.facts dd {
  margin: 0;
  font-weight: 600;
  word-break: break-all;
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.actions .btn-primary {
  flex: 1;
  min-width: 200px;
}

/* ---------- login ---------- */

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 32px 28px;
  text-align: center;
}

.login-card h1 {
  font-size: 1.4rem;
}

.login-card p {
  margin: -8px 0 4px;
}

.login-card .field {
  width: 100%;
  text-align: left;
}

/* ---------- turnstile ---------- */

.turnstile {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 65px;
}

.verify-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-bottom: 20px;
}

.verify-box p {
  margin: 0;
}

/* ---------- forms ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.field > span {
  font-size: 0.875rem;
  font-weight: 500;
}

.inline-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.875rem;
}

.input-group {
  display: flex;
  gap: 8px;
}

.input-group input {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.85rem;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  cursor: pointer;
}

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

.radio-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  line-height: 1.4;
}

.radio-card input {
  display: none;
}

.radio-card:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}

/* ---------- admin ---------- */

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
}

.stat-label {
  color: var(--muted);
  font-size: 0.85rem;
}

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px 16px;
  text-align: center;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.dragging {
  border-color: var(--primary);
  background: var(--primary-soft);
  outline: none;
}

.dropzone-icon {
  font-size: 2rem;
}

.queue {
  margin-top: 16px;
  display: grid;
  gap: 8px;
}

.queue-head {
  display: flex;
  justify-content: space-between;
}

.queue-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
}

.queue-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}

.queue-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.queue-item.is-done .queue-status {
  color: var(--success);
}

.queue-item.is-error .queue-status {
  color: var(--danger);
}

.queue-item.is-error .progress-bar {
  background: var(--danger);
}

.queue-item.is-done .progress-bar {
  background: var(--success);
}

.progress {
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}

.progress-sm {
  height: 4px;
  margin-top: 4px;
}

.progress-bar {
  height: 100%;
  background: var(--primary);
  transition: width 0.2s;
}

.filters {
  display: flex;
  gap: 8px;
}

.filters input {
  width: 220px;
}

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--primary-soft);
  margin-bottom: 12px;
  font-size: 0.9rem;
}

.table-wrap {
  overflow-x: auto;
  margin: 0 -20px -20px;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.table th,
.table td {
  padding: 10px 12px;
  text-align: left;
  border-top: 1px solid var(--border);
  vertical-align: middle;
}

.table th {
  font-weight: 500;
  color: var(--muted);
  font-size: 0.8rem;
  background: var(--surface-2);
  white-space: nowrap;
}

.table th:first-child,
.table td:first-child {
  padding-left: 20px;
}

.table th:last-child,
.table td:last-child {
  padding-right: 20px;
}

.table tr.selected td {
  background: var(--primary-soft);
}

.table .num {
  text-align: right;
  white-space: nowrap;
}

.col-check {
  width: 36px;
}

.col-actions {
  width: 1%;
}

.row-actions {
  display: flex;
  gap: 2px;
}

.cell-file {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 240px;
}

.cell-file .file-icon {
  width: 36px;
  height: 36px;
  font-size: 1.15rem;
}

.cell-file-text {
  min-width: 0;
  flex: 1;
}

.cell-name {
  font-weight: 600;
  color: var(--text);
  word-break: break-word;
}

.cell-name:hover {
  color: var(--primary);
}

.cell-desc {
  color: var(--muted);
  font-size: 0.8rem;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 500;
  white-space: nowrap;
}

.badge-public {
  background: var(--success-soft);
  color: var(--success);
}

.badge-unlisted {
  background: var(--warn-soft);
  color: var(--warn);
}

.badge-private {
  background: var(--surface-2);
  color: var(--muted);
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

/* ---------- modal & toast ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 20, 0.5);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
  overflow-y: auto;
}

.modal {
  width: 100%;
  max-width: 600px;
  margin: 0;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.toasts {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  max-width: calc(100vw - 40px);
}

.toast {
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  font-size: 0.9rem;
  animation: toast-in 0.2s ease-out;
}

.toast-error {
  background: var(--danger);
  color: #fff;
}

@keyframes toast-in {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
}

/* ---------- responsive ---------- */

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

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

  .radio-cards {
    grid-template-columns: 1fr;
  }

  .topbar-admin .tabs {
    order: 3;
    width: 100%;
    padding-bottom: 8px;
  }
}

@media (max-width: 600px) {
  .file-card {
    flex-direction: column;
    align-items: stretch;
  }

  .btn-download {
    width: 100%;
  }

  .filters {
    width: 100%;
  }

  .filters input {
    width: auto;
    flex: 1;
  }

  .file-detail {
    padding: 20px;
  }

  .topbar-right .topbar-link {
    display: none;
  }
}

/* ---------- PHP pages ---------- */

[hidden] {
  display: none !important;
}

.row-actions form {
  display: contents;
}

.edit-grid {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 20px;
  align-items: start;
}

.edit-grid .progress {
  margin-top: 12px;
}

.facts-list {
  grid-template-columns: 1fr;
  margin: 0;
}

.toast {
  animation: toast-in 0.2s ease-out, toast-out 0.3s ease-in 4s forwards;
}

@keyframes toast-out {
  to {
    opacity: 0;
    visibility: hidden;
  }
}

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

.alert-info {
  background: var(--primary-soft);
  color: var(--text);
}

.home-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 16px;
}

.home-toggle > span:nth-of-type(2) {
  flex: 1;
  min-width: 200px;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.on {
  background: var(--success);
  box-shadow: 0 0 0 4px var(--success-soft);
}

.status-dot.off {
  background: var(--muted);
  box-shadow: 0 0 0 4px var(--surface-2);
}

.switch-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 14px;
  cursor: pointer;
}

.switch-row > span {
  display: flex;
  flex-direction: column;
}

input[type='checkbox'].switch {
  appearance: none;
  -webkit-appearance: none;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  flex-shrink: 0;
  margin-top: 2px;
  transition: background 0.15s;
}

input[type='checkbox'].switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}

input[type='checkbox'].switch:checked {
  background: var(--primary);
}

input[type='checkbox'].switch:checked::after {
  transform: translateX(18px);
}
