:root {
  color-scheme: light;
  --page-bg: #edf0f3;
  --panel-bg: #ffffff;
  --panel-fg: #182029;
  --muted-fg: #66717e;
  --line: #cfd6df;
  --accent: #1266d6;
  --hover: #eef4ff;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--page-bg);
  color: var(--panel-fg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
  min-height: 34px;
  padding: 7px 12px;
  color: var(--panel-fg);
  background: #f7f9fb;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--hover);
  border-color: #a9bdd8;
}

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

.app-header {
  min-height: 64px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--line);
}

.title-group {
  min-width: 260px;
  display: flex;
  align-items: center;
  gap: 12px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 18px;
  line-height: 1;
}

#screenNameInput {
  width: min(34vw, 360px);
  min-height: 34px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-weight: 700;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
}

.stage-panel {
  min-width: 0;
  min-height: 0;
  padding: 24px;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.stage {
  position: relative;
  width: min(100%, calc((100vh - 128px) * 16 / 9));
  aspect-ratio: 16 / 9;
  background: #050505;
  box-shadow: 0 20px 60px rgba(20, 28, 38, 0.22);
  overflow: hidden;
}

.stage-item {
  position: absolute;
  max-width: 92%;
  transform: translate(-50%, -50%);
  color: #ffffff;
  font-weight: 800;
  line-height: 1.12;
  text-align: center;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
  cursor: grab;
  user-select: none;
}

.stage-item.is-image {
  display: grid;
  place-items: center;
}

.stage-item.is-image img {
  display: block;
  width: 100%;
  max-height: 88vh;
  object-fit: contain;
  pointer-events: none;
}

.stage-item.is-selected {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
}

.stage-item.is-editing {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  cursor: text;
  user-select: text;
}

.handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #ffffff;
  border: 1.5px solid var(--accent);
  border-radius: 2px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
  z-index: 20;
}

.handle-nw {
  top: 0;
  left: 0;
  transform: translate(-50%, -50%);
  cursor: nwse-resize;
}

.handle-ne {
  top: 0;
  right: 0;
  transform: translate(50%, -50%);
  cursor: nesw-resize;
}

.handle-se {
  bottom: 0;
  right: 0;
  transform: translate(50%, 50%);
  cursor: nwse-resize;
}

.handle-sw {
  bottom: 0;
  left: 0;
  transform: translate(-50%, 50%);
  cursor: nesw-resize;
}

.snap-guide {
  position: absolute;
  background: #ff3b6b;
  pointer-events: none;
  z-index: 30;
}

.snap-guide-v {
  top: 0;
  bottom: 0;
  width: 1px;
  transform: translateX(-0.5px);
}

.snap-guide-h {
  left: 0;
  right: 0;
  height: 1px;
  transform: translateY(-0.5px);
}

.side-panel {
  min-height: 0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--panel-bg);
  border-left: 1px solid var(--line);
  overflow: auto;
}

.control-section {
  display: grid;
  gap: 10px;
}

.control-section + .control-section {
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.control-section h2 {
  font-size: 14px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted-fg);
  font-size: 12px;
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  min-height: 34px;
  padding: 7px 9px;
  color: var(--panel-fg);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 6px;
}

textarea {
  resize: vertical;
}

input[type="color"] {
  padding: 3px;
}

input[type="range"] {
  padding: 0;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.selection-label {
  min-height: 24px;
  padding: 5px 8px;
  color: var(--muted-fg);
  background: #f5f7fa;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 800;
}

.order-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.order-row button {
  padding: 6px 4px;
  font-size: 12px;
}

.shortcut-hint {
  color: var(--muted-fg);
  font-size: 11px;
  line-height: 1.6;
  font-weight: 600;
}

.context-menu {
  position: fixed;
  min-width: 200px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(20, 28, 38, 0.24);
  z-index: 1000;
}

.context-menu button {
  display: flex;
  align-items: center;
  min-height: 32px;
  padding: 6px 10px;
  background: transparent;
  border: 0;
  border-radius: 5px;
  font-weight: 600;
  text-align: left;
}

.context-menu button:hover:not(:disabled) {
  background: var(--hover);
  border-color: transparent;
}

.context-menu button:disabled {
  opacity: 0.4;
}

.context-menu button.danger {
  color: #c0263b;
}

.context-menu .menu-key {
  margin-left: auto;
  padding-left: 18px;
  color: var(--muted-fg);
  font-size: 11px;
  font-weight: 700;
}

.context-menu hr {
  margin: 4px 2px;
  border: 0;
  border-top: 1px solid var(--line);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 10px 16px;
  background: #182029;
  color: #ffffff;
  border-radius: 8px;
  font-weight: 700;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 2000;
}

.toast.show {
  opacity: 1;
}

@media (max-width: 880px) {
  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .title-group,
  .toolbar {
    width: 100%;
  }

  #screenNameInput {
    width: 100%;
  }

  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(280px, 1fr) auto;
  }

  .side-panel {
    border-left: 0;
    border-top: 1px solid var(--line);
  }
}

.waiting-room-section form, .waiting-room-actions { display:grid; gap:10px; }
.waiting-room-actions { margin-top:12px; }
#applyWaitingScreen { background:var(--accent); border-color:var(--accent); color:#fff; }
.waiting-room-section .waiting-live-toggle { display:flex; align-items:center; gap:8px; }
.waiting-live-toggle input { width:auto; }
#waitingRoomStatus { font-size:13px; line-height:1.5; color:var(--muted-fg); overflow-wrap:anywhere; }
#waitingRoomStatus.is-error { color:#b3261e; }
