/* --- Tokens --- */
:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --border: #d9dce7;
  --text: #1f2430;
  --muted: #5b6173;
  --accent: #0f62fe;
  --success: #0b8a56;
  --warn: #d47300;
  --error: #c62828;
  /* Simple layout: equal inset from viewport on all sides (tune in one place) */
  --simple-page-inset: 20px;
}

/* --- Reset / base --- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
}

body {
  margin: 0;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation; /* 防止雙擊縮放 */
  -webkit-tap-highlight-color: transparent; /* 移除點擊高亮 */
}

/* --- App shell --- */
#app {
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

/* 防止內容未加載完成時的閃爍和寬度跳動 */
#app:empty {
  min-height: 100vh;
  min-height: 100dvh;
}

#app > * {
  max-width: 100vw;
  overflow-x: hidden;
}

/* --- Default page (centered column, max content width) --- */
.page {
  max-width: min(1200px, 100vw);
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px 40px;
  overflow-x: hidden;
}

/* --- Simple layout: split video / subtitle (flex or grid via data-layout) --- */
.page--simple {
  max-width: 100vw;
  width: 100vw;
  height: 100vh;
  height: 100dvh; /* 動態視窗高度，考慮移動端瀏覽器地址欄 */
  overflow: hidden;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
  padding: 12px;
  box-sizing: border-box;
}

.video-container {
  width: auto;
  flex: 3 1 0;
  min-width: 0;
  max-height: none;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
}

.subtitle-container {
  width: auto;
  flex: 2 1 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: stretch;
}

.video-container .video-panel,
.subtitle-container .subtitle-panel {
  width: 100%;
  max-width: 100%;
}

.video-panel,
.subtitle-panel {
  max-width: 100%;
  overflow-x: hidden;
}

.video-container .video-panel {
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-container video {
  width: 100%;
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px); /* 動態視窗高度 */
  object-fit: contain;
  aspect-ratio: 16 / 9;
}

.video-container .no-signal-message {
  width: 100%;
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px); /* 動態視窗高度 */
  aspect-ratio: 16 / 9;
}

.subtitle-container .subtitle-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Stretch caption in default stacked subtitle column */
.subtitle-container .caption {
  flex-grow: 1;
  height: auto !important;
  min-height: 0;
}

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

h1 {
  margin: 0;
  font-size: 28px;
}

button {
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  background: #0d52d4;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  max-width: 100%;
}

/* 確保所有視頻元素不會超出容器 */
video {
  max-width: 100%;
  height: auto;
}

.field {
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}

.field label {
  font-weight: 600;
  margin-bottom: 6px;
}

input[type="text"] {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 15px;
}

/* --- Default grid layout (two columns) --- */
.layout {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 16px;
  margin-top: 16px;
  max-width: 100%;
  overflow-x: hidden;
}

/* Simple + overlay: single column; driven by #app[data-layout="simple"] */
#app[data-layout="simple"] .layout {
  display: flex !important;
  flex-direction: column !important;
  grid-template-columns: none;
  align-items: center;
  gap: 16px;
}

#app[data-layout="simple"] .page--simple .layout {
  margin-top: 0;
  padding: 20px 0;
  min-height: 100vh;
  min-height: 100dvh; /* 動態視窗高度 */
}

.video-panel video {
  width: 100%;
  max-width: 100%;
  min-height: 320px;
  background: #0d1117;
  border-radius: 10px;
  border: 1px solid var(--border);
  pointer-events: none;
  user-select: none;
}

.page--simple .video-panel {
  width: 100%;
  max-width: none;
  flex-shrink: 0;
}

.page--simple .video-panel video {
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  min-height: 0;
  border: none;
  border-radius: 10px;
}

.page--simple .subtitle-panel {
  width: 100%;
  max-width: none;
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}

.page--simple .caption {
  width: 100%;
}

/* Final simple-layout overrides:
   - desktop/landscape: video left + subtitle right (3:2)
   - mobile/portrait: stacked layout */
#app[data-layout="simple"] .page--simple {
  display: grid;
  grid-template-columns: 3fr 2fr;
  grid-template-rows: auto minmax(0, 1fr);
  align-items: stretch;
  justify-content: stretch;
  gap: 16px;
  padding: var(--simple-page-inset);
  min-height: 0;
}

#app[data-layout="simple"] .page--simple .video-container,
#app[data-layout="simple"] .page--simple .subtitle-container {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

#app[data-layout="simple"] .page--simple .video-container {
  grid-column: 1;
  grid-row: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

#app[data-layout="simple"] .page--simple .video-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
}

#app[data-layout="simple"] .page--simple .video-panel video,
#app[data-layout="simple"] .page--simple .no-signal-message {
  width: 100%;
  max-width: 100%;
  max-height: calc(100dvh - 2 * var(--simple-page-inset) - 228px);
  min-height: 0;
  object-fit: contain;
  flex: 0 1 auto;
}

#app[data-layout="simple"] .page--simple .subtitle-container {
  grid-column: 2;
  grid-row: 1 / -1;
  display: flex;
  align-items: stretch;
  justify-content: center;
}

#app[data-layout="simple"] .page--simple .note-container {
  grid-column: 1;
  grid-row: 2;
  display: flex;
  min-width: 0;
  min-height: 0;
}

#app[data-layout="simple"] .page--simple .note-container .note-area {
  flex: 1;
  margin-top: 0;
}

#app[data-layout="simple"] .page--simple .subtitle-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Grid simple: caption fills column; portrait override below fixes height */
#app[data-layout="simple"] .page--simple .subtitle-panel .caption {
  width: 100%;
  height: auto;
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
}

.page--overlay .layout {
  padding: 0;
  gap: 0;
}

.page--overlay .video-panel video {
  height: 100vh;
  height: 100dvh; /* 動態視窗高度 */
  min-height: 100vh;
  min-height: 100dvh; /* 動態視窗高度 */
  border-radius: 0;
}

.page--overlay .no-signal-message {
  height: 100vh;
  height: 100dvh; /* 動態視窗高度 */
  min-height: 100vh;
  min-height: 100dvh; /* 動態視窗高度 */
  border-radius: 0;
}

.video-panel--overlay {
  position: relative;
}

.subtitle-overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px 18px 20px;
  pointer-events: none;
}

.caption--overlay {
  background: rgba(0, 0, 0, 0.65);
  color: #ffffff;
  border: none;
  font-size: 22px;
  line-height: 1.4;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}

.page--simple .no-signal-message {
  width: 100%;
  aspect-ratio: 16 / 9;
  min-height: 0;
}

.no-signal-message {
  width: 100%;
  min-height: 320px;
  background: #f8f9fa;
  border-radius: 10px;
  border: 1px solid var(--border);
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 600;
  color: var(--muted);
}

.media-toolbar {
  width: 100%;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  flex: 0 0 auto;
  margin-bottom: 12px;
  padding: 6px 8px;
  background: transparent;
  border: none;
}

.toolbar-back-button {
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: #282800;
  color: #ffffff;
  line-height: 1;
}

.toolbar-back-button::before {
  content: "";
  width: 9px;
  height: 9px;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateX(2px) rotate(45deg);
}

.toolbar-back-button:hover,
.toolbar-back-button:focus-visible {
  background: #3a3a12;
  color: #ffffff;
}

.toolbar-back-button:active {
  background: #1f1f00;
}

.mute-toggle-button {
  width: 34px;
  height: 30px;
  min-height: 30px;
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 1px solid #282800;
  border-radius: 8px;
  background: transparent;
  color: #282800;
}

.mute-toggle-button:hover,
.mute-toggle-button:focus-visible {
  background: rgba(40, 40, 0, 0.08);
  color: #282800;
}

.mute-toggle-button[aria-pressed="false"] {
  background: #282800;
  color: #ffffff;
}

.mute-toggle-button[aria-pressed="false"]:hover,
.mute-toggle-button[aria-pressed="false"]:focus-visible {
  background: #3a3a12;
  color: #ffffff;
}

.mute-toggle-button__icon {
  width: 20px;
  height: 20px;
  display: none;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.mute-toggle-button__icon path:first-child {
  fill: currentColor;
  stroke: none;
}

.mute-toggle-button[aria-pressed="true"] .mute-toggle-button__icon--muted,
.mute-toggle-button[aria-pressed="false"] .mute-toggle-button__icon--sound {
  display: block;
}

.note-toggle-button {
  display: none;
}

.note-toggle-button__icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.live-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 3px 10px;
  border-radius: 5px;
  background: #8c9099;
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.live-badge--active {
  background: #d81920;
}

.media-toolbar--subtitle {
  gap: 8px;
  flex-wrap: wrap;
}

.subtitle-mode-button {
  padding: 0;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  box-shadow: none;
  appearance: none;
}

.subtitle-mode-button:hover,
.subtitle-mode-button:focus,
.subtitle-mode-button:focus-visible,
.subtitle-mode-button:active {
  background: transparent;
  color: var(--accent);
  box-shadow: none;
  outline: none;
}

.subtitle-mode-button[aria-pressed="true"] {
  color: var(--accent);
}

.subtitle-mode-separator {
  color: var(--muted);
  user-select: none;
}

.subtitle-font-size-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: 8px;
}

.subtitle-font-size-label {
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  user-select: none;
}

.subtitle-font-size-slider {
  width: 112px;
  accent-color: #282800;
  cursor: pointer;
}

.note-area {
  width: 100%;
  min-height: 160px;
  flex: 1 1 180px;
  margin-top: 16px;
  padding: 14px 16px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8f9fa;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  line-height: 1.5;
  outline: none;
}

.note-area::placeholder {
  color: var(--muted);
}

.note-area:focus {
  border-color: #a7aa79;
  box-shadow: 0 0 0 3px rgba(167, 170, 121, 0.18);
}

.status-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
  color: var(--muted);
}

.label {
  font-weight: 600;
  color: var(--text);
}

/* --- Caption & log --- */
.caption {
  height: 200px;
  padding: 8px 0;
  border-radius: 0;
  background: transparent;
  border: none;
  font-size: 20px;
  font-weight: 500;
  overflow-y: auto;
  white-space: normal;
  word-wrap: break-word;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.caption-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

@keyframes caption-item-enter {
  from { opacity: 0; }
}

@keyframes caption-item-update {
  from { opacity: 0.4; }
}

.caption-item--enter {
  animation: caption-item-enter 0.25s ease-out;
}

.caption-item--update {
  animation: caption-item-update 0.3s ease-out;
}

.caption-added-text {
  animation: caption-item-enter 0.35s ease-out;
}

/* Tentative (provisional) translation tail: not yet frozen by local
   agreement, may still be rewritten — rendered lighter than committed text. */
.caption-tentative {
  opacity: 0.45;
  transition: opacity 0.6s ease;
}

/* No update for a while: the tentative tail is not going to change soon, so
   let it settle into the committed color (see armCaptionIdleTimer). */
.caption-idle .caption-tentative {
  opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
  .caption-item--enter,
  .caption-item--update,
  .caption-added-text {
    animation: none;
  }
  .caption-tentative {
    transition: none;
  }
}

.caption-original,
.caption-translation {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "Noto Serif TC", "Noto Serif", "Times New Roman", Times, serif;
}

.caption-original {
  color: #282800;
  font-weight: 600;
}

.caption-translation {
  color: #66663e;
  font-weight: 500;
}

.log {
  height: 260px;
  overflow-y: auto;
  overflow-x: auto;
  background: #10131a;
  color: #e9ecf5;
  border-radius: 10px;
  border: 1px solid #1f2430;
  margin-top: 8px;
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, SFMono, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px;
  max-width: 100%;
  word-wrap: break-word;
  word-break: break-all;
}

.log div {
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.log div:last-child {
  border-bottom: none;
}

/* --- Media: narrow / portrait --- */
@media (orientation: portrait), (max-width: 900px) {
  .page--simple {
    flex-direction: column;
    align-items: center;
    padding: 12px 0;
  }

  .video-container,
  .subtitle-container {
    width: 90vw;
    flex: 0 0 auto;
  }

  .video-container {
    max-height: 80vh;
    max-height: 80dvh;
  }

  .subtitle-container {
    flex: 1 1 auto;
  }

  .video-container video,
  .video-container .no-signal-message {
    max-height: 80vh;
    max-height: 80dvh;
  }

  #app[data-layout="simple"] .page--simple {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: var(--simple-page-inset);
    grid-template-rows: none;
  }

  #app[data-layout="simple"] .page--simple .video-container {
    align-items: flex-start;
    justify-content: center;
  }

  #app[data-layout="simple"] .page--simple .video-panel {
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
  }

  #app[data-layout="simple"] .page--simple .video-container,
  #app[data-layout="simple"] .page--simple .subtitle-container {
    width: min(90vw, calc(100% - 2 * var(--simple-page-inset)));
    height: auto;
  }

  #app[data-layout="simple"] .page--simple .video-container {
    order: 1;
  }

  #app[data-layout="simple"] .page--simple .subtitle-container {
    order: 2;
    flex: 1 1 0;
    min-height: 0;
    overflow: hidden;
  }

  #app[data-layout="simple"] .page--simple .note-container {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    height: 45vh;
    padding: 16px var(--simple-page-inset);
    background: var(--panel);
    border-top: 1px solid var(--border);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.12);
  }

  #app[data-layout="simple"] .page--simple .note-container.note-container--open {
    display: flex;
  }

  #app[data-layout="simple"] .page--simple .note-container .note-area {
    flex: 1;
    margin-top: 0;
    min-height: 0;
  }

  .note-toggle-button {
    display: inline-flex;
    width: 34px;
    height: 30px;
    min-height: 30px;
    margin-left: auto;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid #282800;
    border-radius: 8px;
    background: transparent;
    color: #282800;
  }

  .note-toggle-button:hover,
  .note-toggle-button:focus-visible {
    background: rgba(40, 40, 0, 0.08);
  }

  .note-toggle-button[aria-expanded="true"] {
    background: #282800;
    color: #ffffff;
  }

  #app[data-layout="simple"] .page--simple .video-panel video,
  #app[data-layout="simple"] .page--simple .no-signal-message {
    max-height: calc(80vh - 48px);
    max-height: calc(80dvh - 48px);
  }

  #app[data-layout="simple"] .page--simple .subtitle-panel .caption {
    flex: 1 1 0;
    min-height: 0;
  }
}

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

/* --- Login (no ?src) & fatal overlay --- */
.login-view,
.fatal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

/* Shown when the URL has no ?src=<key>; owns the viewport while visible. */
.login-view {
  z-index: 100;
}

.login-view__form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 360px;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(15, 20, 40, 0.08);
}

.login-view__title {
  margin: 0 0 4px;
  font-size: 1.25rem;
  color: var(--text);
}

.login-view__input {
  padding: 10px 12px;
  font-size: 1rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}

.login-view__label {
  font-weight: 600;
  color: var(--text);
}

.login-view__input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 98, 254, 0.2);
}

.login-view__button {
  padding: 10px 12px;
  font-size: 1rem;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

.login-view__button:hover {
  filter: brightness(1.05);
}

.login-view__error {
  margin: 0;
  font-size: 0.9rem;
  color: var(--error);
}

.fatal {
  color: var(--error);
  font-size: 1rem;
  text-align: center;
  z-index: 200;
}

.session-expired {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(246, 247, 251, 0.96);
}

.session-expired__panel {
  width: 100%;
  max-width: 360px;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(15, 20, 40, 0.08);
}

.session-expired__title {
  margin: 0 0 8px;
  font-size: 1.25rem;
}

.session-expired__message {
  margin: 0 0 16px;
  color: var(--muted);
}

.session-expired__button {
  width: 100%;
}

.rate-limit-dialog {
  position: fixed;
  inset: 0;
  z-index: 320;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(246, 247, 251, 0.96);
}

.rate-limit-dialog__panel {
  width: 100%;
  max-width: 360px;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(15, 20, 40, 0.08);
}

.rate-limit-dialog__title {
  margin: 0 0 12px;
  font-size: 1.25rem;
}

.rate-limit-dialog__message {
  margin: 0 0 16px;
  color: var(--muted);
}

.rate-limit-dialog__button {
  width: 100%;
  padding: 10px 12px;
  font-size: 1rem;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

.rate-limit-dialog__button:hover {
  filter: brightness(1.05);
}
