* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: #111;
  color: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  height: 100%;
  --recording-bar-offset: 56px;
}

.app-header {
  text-align: center;
  padding: 16px;
  border-bottom: 1px solid #333;
}

.app-header h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.1em;
}

.app-main {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
}

/* ---------- Editor ---------- */

.editor {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#text-input {
  width: 100%;
  min-height: 240px;
  resize: vertical;
  background: #1a1a1a;
  color: #fff;
  border: 1px solid #333;
  border-radius: 6px;
  padding: 12px;
  font-size: 16px;
  line-height: 1.5;
}

.text-tools,
.transport {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.transport {
  position: relative;
  z-index: 5;
}

button {
  background: #2a2a2a;
  color: #fff;
  border: 1px solid #444;
  border-radius: 6px;
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: #3a3a3a;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#btn-start {
  background: #1f6f43;
  border-color: #1f6f43;
}

#btn-start:hover:not(:disabled) {
  background: #278a54;
}

#btn-preview {
  background: #1a4a6f;
  border-color: #1a4a6f;
}

#btn-preview:hover:not(:disabled) {
  background: #256399;
}

#btn-preview-approve {
  background: #1f6f43;
  border-color: #1f6f43;
}

#btn-preview-approve:hover:not(:disabled) {
  background: #278a54;
}

.settings {
  display: flex;
  flex-direction: column;
  gap: 12px;
  border: 1px solid #333;
  border-radius: 6px;
  padding: 16px;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.settings-row label {
  min-width: 110px;
  font-size: 14px;
  color: #ccc;
}

.settings-row input[type="range"] {
  flex: 1;
}

.settings-value {
  min-width: 48px;
  text-align: right;
  font-size: 14px;
  color: #aaa;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: unset;
  cursor: pointer;
}

/* ---------- AdSense (só no modo edição) ---------- */

.ad-slot-container {
  position: relative;
  z-index: 1;
  border: 1px dashed #444;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  overflow: hidden;
  margin-top: 8px;
  isolation: isolate;
  contain: layout paint;
}

.ad-slot-label {
  margin: 0;
  padding: 12px 16px 0;
  text-align: center;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: #888;
}

#adsense-slot {
  display: block;
  min-height: 280px;
  padding: 0 8px 12px;
  overflow: hidden;
  position: relative;
}

/* ---------- Reader (modo leitura) ---------- */

body.mode-read .editor {
  display: none;
}

body.mode-edit .reader {
  display: none;
}

body.mode-preview .editor {
  display: none;
}

body.mode-preview .app-header {
  display: none;
}

body.mode-preview #btn-record,
body.mode-preview #btn-stop,
body.mode-preview #btn-download {
  opacity: 0.4;
  pointer-events: none;
}

.reader {
  --reader-column-width: 50ch;
  --reader-band-height: 25vh;
  --reader-text-align: center;

  position: fixed;
  inset: 0;
  background: #000;
  z-index: 10;
}

.reader-viewport {
  height: 100%;
  overflow-y: hidden;
  padding-top: 0;
  padding-bottom: calc(100vh - var(--reader-band-height));
}

.reader-text {
  max-width: var(--reader-column-width);
  margin: 0 auto;
  text-align: var(--reader-text-align);
  color: #fff;
  font-size: 42px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.reader--focus-mask::after {
  content: '';
  position: fixed;
  left: 0;
  right: 0;
  top: var(--reader-band-height);
  bottom: 0;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.35) 0%, rgba(0, 0, 0, 0.92) 40%);
  z-index: 11;
}

/* ---------- Barra de gravação ---------- */

body.mode-edit .recording-bar {
  display: none;
}

.recording-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.85);
  border-top: 1px solid #333;
  z-index: 20;
}

#btn-record {
  border-color: #7a1f1f;
}

.scroll-controls {
  display: flex;
  gap: 8px;
  padding-right: 12px;
  margin-right: 4px;
  border-right: 1px solid #444;
}

.scroll-controls[hidden] {
  display: none;
}

#btn-back {
  border-color: #555;
}

#btn-pause {
  border-color: #555;
}

#btn-reset {
  border-color: #555;
}

.rec-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  font-size: 14px;
}

.rec-dot {
  color: #ff4d4d;
  font-weight: bold;
  animation: blink 1s step-start infinite;
}

@keyframes blink {
  50% { opacity: 0.2; }
}

/* ---------- Preview de webcam ---------- */

.webcam-preview {
  position: fixed;
  right: 16px;
  bottom: calc(var(--recording-bar-offset, 70px) + env(safe-area-inset-bottom, 0px));
  width: 160px;
  height: 120px;
  object-fit: cover;
  border: 2px solid #444;
  border-radius: 6px;
  background: #000;
  z-index: 15;
}

/* ---------- Pré-visualização ---------- */

.preview-bar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: rgba(26, 74, 111, 0.95);
  border-bottom: 1px solid #256399;
  z-index: 25;
}

.preview-bar.is-visible {
  display: flex;
}

.preview-hint {
  flex: 1;
  font-size: 14px;
  color: #cce0f0;
}

/* ---------- Countdown ---------- */

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

.countdown-overlay.is-visible {
  display: flex;
}

.countdown-number {
  font-size: 120px;
  font-weight: bold;
  color: #fff;
  animation: countdown-pop 0.9s ease-out;
}

@keyframes countdown-pop {
  0% { transform: scale(1.4); opacity: 0.3; }
  50% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.9); opacity: 0.6; }
}

body.mode-preview .reader-viewport {
  padding-top: 52px;
}

body.mode-preview .reader--focus-mask::after {
  top: calc(var(--reader-band-height) + 52px);
}

/* ---------- Mobile: barra de gravação ---------- */

@media (max-width: 640px) {
  .app-main {
    padding-bottom: max(24px, env(safe-area-inset-bottom));
  }

  .transport {
    position: sticky;
    bottom: 0;
    z-index: 10;
    margin: 0 -16px;
    padding: 12px 16px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: linear-gradient(to top, #111 70%, rgba(17, 17, 17, 0));
  }

  .transport button {
    flex: 1 1 calc(50% - 4px);
    min-height: 44px;
    touch-action: manipulation;
  }

  .ad-slot-container {
    margin-bottom: 8px;
  }

  body:not(.mode-edit) {
    --recording-bar-offset: 118px;
  }

  .recording-bar {
    gap: 6px;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  .recording-bar > button {
    flex: 1 1 calc(50% - 3px);
    min-width: 0;
    padding: 9px 10px;
    font-size: 13px;
  }

  .scroll-controls {
    flex: 1 1 100%;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid #444;
    padding-right: 0;
    padding-bottom: 8px;
    margin-right: 0;
  }

  .scroll-controls > button {
    flex: 1 1 auto;
    min-width: 0;
  }

  .rec-indicator {
    flex: 1 1 100%;
    margin-left: 0;
    justify-content: center;
  }
}
