:root {
  --primary: #0055ff;
  --bg: #ffffff;
  --bg-page: #f0f0f0;
  --surface: #f7f7f7;
  --border: #e1e1e1;
  --text: #111111;
  --text-secondary: #555555;
  --text-tertiary: #999999;
  --danger: #dc2626;
  --radius: 10px;
  --radius-sm: 8px;
  --topbar-height: 58px;
}

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

html, body {
  height: 100%;
  font-family: Inter, -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-page);
}

[hidden] { display: none !important; }

.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--topbar-height);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 22px;
  z-index: 100;
}

.topbar-brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  text-decoration: none;
  min-width: 120px;
  margin-right: 32px;
}

.topbar-brand span { color: var(--primary); }

.topbar-nav {
  display: flex;
  align-items: center;
  gap: 22px;
  flex: 1;
  align-self: stretch;
}

.topbar-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  text-decoration: none;
  font-weight: 500;
  position: relative;
}

.topbar-link.active { color: var(--text); }
.topbar-link.active::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: var(--primary);
}

.topbar-account { position: relative; margin-left: auto; }

.topbar-avatar {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  background: none;
}

.topbar-avatar img { width: 100%; height: 100%; object-fit: cover; }

.avatar-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  min-width: 160px;
  display: none;
  z-index: 200;
}

.avatar-menu.show { display: block; }

.avatar-menu a,
.avatar-menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  color: var(--text);
  background: none;
  border: 0;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
}

.avatar-menu a:hover,
.avatar-menu button:hover { background: var(--surface); }

.avatar-menu-form { margin: 0; }

.main-content {
  padding-top: var(--topbar-height);
  height: 100vh;
  overflow: hidden;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-group { margin-bottom: 16px; }

input.form-input,
textarea.form-input,
.form-select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  color: var(--text);
  background: var(--bg);
  outline: none;
}

textarea.form-input {
  min-height: 140px;
  resize: vertical;
}

textarea.form-input-sm {
  min-height: 88px;
}

.form-input:focus,
.form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 85, 255, 0.08);
}

.form-group.is-invalid .form-input,
.form-group.is-invalid .form-select,
.form-group.is-invalid .photo-drop,
.form-group.is-invalid .photo-add,
.form-group.is-invalid .photo-tile {
  border-color: var(--danger);
  background: #fff6f6;
}

.field-error {
  min-height: 18px;
  color: var(--danger);
  font-size: 12px;
  margin-top: 6px;
}

.form-hint {
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 6px;
}

.form-hint a { color: var(--primary); font-weight: 600; text-decoration: none; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 20px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-secondary { background: #f3f4f6; color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  display: inline-block;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.talk-page {
  display: grid;
  grid-template-columns: minmax(560px, 880px) minmax(360px, 1fr);
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.talk-form,
.talk-preview {
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.talk-form {
  background: var(--bg);
  border-right: 1px solid var(--border);
}

.talk-form-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 32px 40px 48px;
}

.page-title { font-size: 24px; letter-spacing: -0.4px; margin-bottom: 6px; }
.page-lede { color: var(--text-secondary); margin-bottom: 22px; }

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 148px));
  gap: 10px;
}

.photo-tile,
.photo-add {
  position: relative;
  aspect-ratio: 3 / 4;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.photo-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.photo-add {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  padding: 8px;
}

.photo-add input { display: none; }
.photo-add i { font-size: 22px; }
.photo-add span { font-size: 12px; line-height: 1.3; }

.photo-tile .photo-remove {
  opacity: 1;
}

.photo-drop {
  position: relative;
  display: block;
  height: 220px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  cursor: pointer;
}

.photo-drop input { display: none; }

.photo-empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--primary);
  font-weight: 600;
}

.photo-empty i { font-size: 28px; }

#photo-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.photo-remove {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: rgba(0,0,0,.6);
  color: #fff;
  cursor: pointer;
}

.audio-drop {
  position: relative;
  display: block;
  min-height: 92px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 14px;
}

.audio-drop input { display: none; }

.audio-empty {
  width: 100%;
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--primary);
  font-weight: 600;
  background: none;
  border: 0;
  cursor: pointer;
  font: inherit;
}

.audio-empty i { font-size: 22px; }

.audio-ready {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-right: 36px;
}

.audio-ready audio { width: 100%; }
.audio-name {
  color: var(--text-secondary);
  font-size: 12px;
  word-break: break-all;
}

.form-group.is-invalid .audio-drop {
  border-color: var(--danger);
  background: #fff6f6;
}

.script-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.script-count { color: var(--text-tertiary); font-size: 12px; margin-top: 6px; }

.talk-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.talk-preview {
  padding: 32px 40px;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.talk-preview h2 { font-size: 16px; margin-bottom: 16px; }

.preview-frame {
  width: min(340px, 100%);
  aspect-ratio: 9 / 16;
  background: #111;
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #bbb;
  position: relative;
}

.preview-empty,
.preview-busy {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  padding: 20px;
}

.preview-empty i { font-size: 36px; }

#preview-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

.preview-status {
  margin-top: 12px;
  color: var(--text-secondary);
}

.page-history,
.page-settings {
  height: 100%;
  overflow: auto;
  padding: 32px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
  margin-bottom: 20px;
}

.history-count { color: var(--text-secondary); margin-top: 4px; }

.search-wrap {
  position: relative;
  min-width: 240px;
}

.search-wrap i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-tertiary);
}

.search-input {
  width: 100%;
  padding: 9px 12px 9px 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.video-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.video-thumb {
  width: 100%;
  aspect-ratio: 9 / 16;
  border: 0;
  background: #111;
  cursor: pointer;
  padding: 0;
}

.video-thumb img,
.video-thumb-placeholder {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
}

.video-info { padding: 12px; }
.video-title { font-weight: 600; margin-bottom: 4px; }
.video-meta { color: var(--text-tertiary); font-size: 12px; }
.video-actions { display: flex; gap: 8px; padding: 0 12px 12px; }
.video-actions .btn { width: auto; flex: 1; padding: 8px 10px; }

.empty-state {
  min-height: 280px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-tertiary);
}

.settings-container {
  max-width: 560px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.settings-section { margin-bottom: 28px; }
.settings-section-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 16px;
}

.settings-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.input-with-action { position: relative; }
.input-with-action input { padding-right: 60px; }
.input-action {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: 0;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
}

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 22px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
}

.toast.show { opacity: 1; }
.toast.success { background: #065f46; color: #fff; }
.toast.error { background: var(--danger); color: #fff; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
}

.modal-overlay.show { display: flex; }

.modal,
.video-modal {
  background: var(--bg);
  border-radius: var(--radius);
  padding: 20px;
  width: min(420px, calc(100% - 32px));
}

.video-modal video {
  width: 100%;
  border-radius: 8px;
  background: #000;
  margin-bottom: 12px;
}

.modal-title { font-weight: 700; margin-bottom: 8px; }
.modal-text { color: var(--text-secondary); margin-bottom: 16px; }
.modal-actions { display: flex; gap: 8px; }
.modal-actions .btn { width: auto; flex: 1; }
.modal-close {
  float: right;
  border: 0;
  background: none;
  font-size: 18px;
  cursor: pointer;
}

.page-login { background: var(--bg-page); }
.login-shell {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.login-eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.login-title { font-size: 28px; margin-bottom: 6px; }
.login-lede { color: var(--text-secondary); margin-bottom: 22px; }

@media (max-width: 960px) {
  .talk-page {
    grid-template-columns: 1fr;
    height: auto;
    overflow: visible;
  }
  .talk-form,
  .talk-preview {
    height: auto;
    overflow: visible;
    display: block;
  }
  .talk-form { border-right: 0; border-bottom: 1px solid var(--border); }
  .main-content { overflow: auto; height: auto; min-height: 100vh; }
  .talk-form-scroll { height: auto; overflow: visible; padding: 24px 20px 40px; }
}
