/* === Base Styles === */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #1a5f7a;
  --primary-dark: #134b61;
  --secondary: #57837b;
  --success: #2d6a4f;
  --danger: #c1292e;
  --warning: #f4a261;
  --light: #f8f9fa;
  --dark: #212529;
  --gray: #6c757d;
  --border: #dee2e6;
  --shadow: rgba(0, 0, 0, 0.1);
  --font-main: 'Noto Sans JP', -apple-system, BlinkMacSystemFont, sans-serif;
}

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: var(--font-main);
  background: var(--light);
  color: var(--dark);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  overflow-x: hidden;
}

/* === Screens === */
.screen {
  display: none;
  min-height: 100vh;
  padding: 20px;
}

.screen.active {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.container {
  width: 100%;
  max-width: 480px;
  text-align: center;
}

/* === Typography === */
.title {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 8px;
}

.subtitle {
  font-size: 1rem;
  color: var(--gray);
  margin-bottom: 40px;
}

.hint {
  font-size: 0.9rem;
  color: var(--gray);
  margin-bottom: 20px;
}

h2 {
  font-size: 1.4rem;
  color: var(--primary);
  margin-bottom: 10px;
}

small {
  font-size: 0.75em;
  opacity: 0.8;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 24px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.97);
}

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

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

.btn-primary:disabled {
  background: var(--gray);
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--secondary);
  color: white;
}

.btn-success {
  background: var(--success);
  color: white;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-large {
  width: 100%;
  padding: 20px;
  font-size: 1.2rem;
}

.btn-small {
  padding: 8px 16px;
  font-size: 0.85rem;
}

.btn-icon-only {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 8px;
}

.btn-back {
  position: absolute;
  top: 20px;
  left: 20px;
  background: none;
  border: none;
  font-size: 1rem;
  color: var(--primary);
  cursor: pointer;
}

.btn-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--gray);
}

.btn-tool {
  background: var(--primary);
  color: white;
  padding: 12px 16px;
  min-width: 100px;
}

/* === Role Selection === */
.role-buttons {
  display: flex;
  gap: 20px;
  justify-content: center;
}

.role-buttons .btn {
  width: 140px;
  height: 140px;
  padding: 20px;
}

.btn-icon {
  font-size: 2.5rem;
  margin-bottom: 8px;
}

.btn-label {
  font-size: 1rem;
  line-height: 1.4;
}

/* === Room Code === */
.room-code-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin: 30px 0;
}

#room-code {
  font-size: 3rem;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  letter-spacing: 8px;
  color: var(--primary);
  background: white;
  padding: 20px 30px;
  border-radius: 12px;
  box-shadow: 0 2px 8px var(--shadow);
}

/* === Code Input === */
.code-input-container {
  margin: 30px 0;
}

#join-code {
  font-size: 2.5rem;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  letter-spacing: 8px;
  text-align: center;
  width: 100%;
  max-width: 280px;
  padding: 16px;
  border: 3px solid var(--primary);
  border-radius: 12px;
  outline: none;
}

#join-code:focus {
  border-color: var(--primary-dark);
  box-shadow: 0 0 0 3px rgba(26, 95, 122, 0.2);
}

/* === Language Select === */
.language-select {
  margin: 20px 0;
  text-align: left;
}

.language-select label {
  display: block;
  font-size: 0.9rem;
  color: var(--gray);
  margin-bottom: 8px;
}

.language-select select {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: white;
}

/* === Status Message === */
.status-message {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 20px;
  padding: 16px;
  background: rgba(26, 95, 122, 0.1);
  border-radius: 8px;
  color: var(--primary);
}

.status-message.hidden {
  display: none;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

/* === Main Screen Layout === */
#screen-main {
  padding: 0;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  background: #f5f5f5;
}

#screen-main.active {
  display: flex;
  flex-direction: column;
}

.main-layout {
  display: flex;
  flex-direction: column;
  flex: 1;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

/* === Header === */
.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--primary);
  color: white;
  flex-shrink: 0;
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--warning);
}

.indicator.connected {
  background: #4ade80;
}

.indicator.error {
  background: var(--danger);
}

.room-info {
  font-family: monospace;
  font-size: 0.9rem;
}

/* === Video Area === */
.video-area {
  position: fixed;
  top: 60px;
  right: 10px;
  left: auto;
  background: rgba(0,0,0,0.9);
  padding: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border-radius: 12px;
  z-index: 200;
  cursor: move;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}

.video-area.dragging {
  opacity: 0.8;
  transform: scale(1.02);
}

/* Drag handle indicator */
.video-area::before {
  content: '⋮⋮';
  display: block;
  text-align: center;
  color: rgba(255,255,255,0.5);
  font-size: 12px;
  letter-spacing: 2px;
  margin-bottom: 4px;
  cursor: move;
}

.video-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#remote-video {
  width: 160px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  background: #222;
  display: block;
}

#local-video {
  width: 160px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  border: 3px solid #4ade80;
  background: #222;
  display: block;
}

.video-area > .btn {
  padding: 6px 10px;
  font-size: 0.75rem;
  width: 100%;
}

/* === Subtitle Area === */
.subtitle-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  padding-bottom: 180px; /* Space for fixed buttons */
  background: #f5f5f5;
  min-height: 0;
  width: 100%;
}

#subtitle-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.subtitle-placeholder {
  text-align: center;
  color: var(--gray);
  padding: 40px 20px;
}

.subtitle-placeholder .small {
  font-size: 0.85rem;
  margin-top: 8px;
}

/* === Subtitle Bubbles === */
.subtitle-bubble {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 16px;
  background: var(--light);
  animation: fadeIn 0.3s ease;
  position: relative;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.subtitle-bubble.self {
  align-self: flex-end;
  background: var(--primary);
  color: white;
  border-bottom-right-radius: 4px;
}

.subtitle-bubble.self.translated {
  background: #2d5a6b;
  border-left: 4px solid #4ade80;
}

.subtitle-bubble.other {
  align-self: flex-start;
  background: #e8f4f8;
  color: #1a1a1a;
  border-bottom-left-radius: 4px;
}

.lang-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  margin-bottom: 6px;
  background: rgba(255,255,255,0.2);
}

.subtitle-bubble.other .lang-badge {
  background: rgba(0,0,0,0.1);
  color: #555;
}

.subtitle-content {
  font-size: 1.2rem;
  font-weight: 500;
  line-height: 1.5;
}

.subtitle-main {
  font-size: 1.2rem;
  font-weight: 500;
  margin-bottom: 6px;
}

.subtitle-sub {
  font-size: 1rem;
  opacity: 0.85;
  line-height: 1.4;
}

/* === Highlight Area === */
.highlight-area {
  padding: 8px 16px;
  background: #fff3cd;
  border-top: 2px solid var(--warning);
  flex-shrink: 0;
}

.highlight-area.hidden {
  display: none;
}

#highlight-content {
  font-size: 1.2rem;
  font-weight: 600;
  text-align: center;
}

/* === Staff Tools === */
.staff-tools {
  position: fixed;
  bottom: 80px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 10px;
  background: rgba(248,249,250,0.98);
  z-index: 500;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
}

.staff-tools.hidden {
  display: none;
}

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

/* === Push-to-Talk === */
.ptt-area {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--primary);
  z-index: 100;
}

.btn-ptt {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  padding: 18px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 16px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.2s ease;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.btn-ptt:active,
.btn-ptt.active {
  background: var(--danger);
  transform: scale(0.98);
}

.ptt-icon {
  font-size: 1.5rem;
}

.ptt-label {
  text-align: left;
  line-height: 1.3;
}

/* === Modals === */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 24px;
}

.modal-large {
  max-width: 600px;
}

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

.modal-header h3 {
  font-size: 1.2rem;
  color: var(--primary);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 20px;
}

.confirm-text {
  text-align: center;
  font-size: 1.1rem;
  margin-bottom: 12px;
}

.notice {
  text-align: center;
  color: var(--gray);
  font-size: 0.9rem;
}

.error-text {
  text-align: center;
  color: var(--danger);
  margin-bottom: 20px;
}

/* === Phrases Panel === */
.phrases-layout {
  display: flex;
  gap: 16px;
}

.phrases-categories {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.category-btn {
  padding: 12px 16px;
  background: var(--light);
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  font-size: 0.9rem;
  transition: all 0.2s ease;
}

.category-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.phrases-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.phrase-item {
  padding: 16px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  text-align: left;
  cursor: pointer;
  transition: all 0.2s ease;
}

.phrase-item:active {
  transform: scale(0.98);
  background: var(--primary-dark);
}

.phrase-ja {
  font-size: 1rem;
  margin-bottom: 4px;
}

.phrase-translated {
  font-size: 0.85rem;
  opacity: 0.85;
}

/* === Confirm Panel === */
.confirm-items {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.confirm-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: var(--light);
  border-radius: 8px;
}

.confirm-check {
  width: 24px;
  height: 24px;
  color: var(--success);
  font-size: 1.2rem;
}

.confirm-text-item {
  flex: 1;
}

.confirm-ja {
  font-size: 1rem;
  margin-bottom: 4px;
}

.confirm-en {
  font-size: 0.85rem;
  color: var(--gray);
}

.confirm-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* === Debug Log === */
.debug-log {
  display: none; /* Hidden by default */
  position: fixed;
  bottom: 160px;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.9);
  color: #0f0;
  font-family: monospace;
  font-size: 10px;
  max-height: 100px;
  z-index: 50;
}

.debug-log.show {
  display: block;
}

.debug-log.collapsed .debug-content {
  display: none;
}

.debug-header {
  display: flex;
  justify-content: space-between;
  padding: 4px 8px;
  background: #333;
}

.debug-header button {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
}

.debug-content {
  padding: 8px;
  max-height: 120px;
  overflow-y: auto;
}

.debug-content div {
  margin-bottom: 2px;
}

/* === Responsive === */
@media (max-width: 480px) {
  .title {
    font-size: 1.5rem;
  }

  .role-buttons {
    flex-direction: column;
    gap: 16px;
  }

  .role-buttons .btn {
    width: 100%;
    height: auto;
    flex-direction: row;
    justify-content: flex-start;
    gap: 16px;
  }

  .btn-icon {
    font-size: 2rem;
    margin-bottom: 0;
  }

  .btn-label {
    text-align: left;
  }

  #room-code {
    font-size: 2.2rem;
    letter-spacing: 4px;
    padding: 16px 20px;
  }

  .phrases-layout {
    flex-direction: column;
  }

  .phrases-categories {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .category-btn {
    flex: 1;
    min-width: 80px;
    text-align: center;
  }

  /* Mobile: Video area small and top-right */
  .video-area {
    top: 50px;
    right: 8px;
    left: auto;
    padding: 6px;
    gap: 4px;
  }

  .video-container {
    flex-direction: row;
    gap: 4px;
  }

  #remote-video,
  #local-video {
    width: 60px;
    height: 45px;
    border-radius: 4px;
  }

  #local-video {
    border-width: 2px;
  }

  .video-area > .btn {
    display: none;
  }

  /* Mobile: Subtitle area full width */
  .subtitle-area {
    padding: 12px;
    padding-top: 10px;
    padding-bottom: 150px;
  }

  .subtitle-bubble {
    max-width: 90%;
    padding: 12px;
  }

  .subtitle-original {
    font-size: 1.1rem;
  }

  /* Mobile: Staff tools - above PTT button */
  .staff-tools {
    bottom: 65px;
    padding: 8px 12px;
    gap: 10px;
    z-index: 500;
  }

  .btn-tool {
    padding: 12px 24px;
    font-size: 1rem;
    min-width: 90px;
    border-radius: 10px;
  }

  /* Mobile: PTT button */
  .ptt-area {
    padding: 8px 12px;
  }

  .btn-ptt {
    padding: 14px;
    font-size: 1rem;
  }

  .ptt-icon {
    font-size: 1.2rem;
  }
}

/* === Tablet and Desktop === */
@media (min-width: 768px) {
  .video-area {
    top: 60px;
    right: 15px;
    left: auto;
  }

  #remote-video,
  #local-video {
    width: 180px;
    height: 135px;
  }

  .subtitle-area {
    padding: 24px;
    padding-bottom: 180px;
  }

  .subtitle-bubble {
    max-width: 80%;
  }

  .subtitle-content {
    font-size: 1.4rem;
  }

  .lang-badge {
    font-size: 0.75rem;
    padding: 3px 10px;
  }
}

/* === Hide debug in production === */
body.production .debug-log {
  display: none;
}
