* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    background: #f3f4f6;
    color: #1f2937;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
}
button, input, select, textarea { font: inherit; }
button { touch-action: manipulation; }

.app-header {
    position: sticky;
    top: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    background: #111827;
    color: #fff;
    min-height: 58px;
}
.header-subtitle { margin-top: 2px; color: #cbd5e1; font-size: 12px; max-width: 58vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-right { display: flex; align-items: center; gap: 10px; }
.logout-link { color: #cbd5e1; font-size: 12px; }
.status-badge { display: inline-block; padding: 5px 8px; border-radius: 999px; font-size: 11px; font-weight: bold; }
.status-badge.online { background: #166534; color: #fff; }
.status-badge.offline { background: #991b1b; color: #fff; }

.tabs {
    position: sticky;
    top: 58px;
    z-index: 40;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    background: #fff;
    border-bottom: 1px solid #d1d5db;
}
.tab-button {
    appearance: none;
    border: 0;
    border-bottom: 3px solid transparent;
    background: #fff;
    padding: 13px 8px 10px;
    font-weight: bold;
    color: #4b5563;
}
.tab-button.active { color: #111827; border-bottom-color: #111827; }
.admin-main { max-width: 820px; margin: 0 auto; padding: 12px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.panel {
    margin: 0 0 12px;
    padding: 14px;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 9px;
    box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.panel h2, .panel h3 { margin-top: 0; }
.sticky-controls { position: relative; }

label { display: block; margin-bottom: 6px; font-weight: bold; }
input[type="text"], input[type="password"], input[type="search"], select, textarea {
    width: 100%;
    padding: 11px;
    border: 1px solid #9ca3af;
    border-radius: 7px;
    background: #fff;
    color: #111827;
}
textarea { resize: vertical; line-height: 1.4; }
input[type="range"] { width: 100%; min-height: 36px; }
input[type="color"] { width: 100%; height: 44px; padding: 3px; border: 1px solid #9ca3af; border-radius: 7px; background: #fff; }

.button {
    appearance: none;
    border: 1px solid #9ca3af;
    border-radius: 7px;
    background: #fff;
    color: #111827;
    padding: 11px 12px;
    font-weight: bold;
    min-height: 44px;
}
.button:active { transform: translateY(1px); }
.button.primary { background: #1d4ed8; border-color: #1d4ed8; color: #fff; }
.button.success { background: #166534; border-color: #166534; color: #fff; }
.button.warning { background: #b45309; border-color: #b45309; color: #fff; }
.button.danger { background: #b91c1c; border-color: #b91c1c; color: #fff; }
.button.large { min-height: 58px; font-size: 18px; }
.button.compact { min-width: 48px; padding-left: 10px; padding-right: 10px; }
.button.full { width: 100%; }
.hold-button { min-height: 68px; user-select: none; -webkit-user-select: none; }
.hold-button.active { background: #111827; color: #fff; }

.button-grid { display: grid; gap: 8px; margin-top: 8px; }
.button-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.button-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.control-inline { display: flex; gap: 8px; align-items: stretch; }
.control-inline > input, .control-inline > select { flex: 1; min-width: 0; }
.control-inline > .button { flex: 0 0 auto; }
.range-with-buttons { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; }
.range-heading { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.range-heading label { margin-bottom: 0; }
.spacer-top { margin-top: 16px; }
.muted { color: #6b7280; }
.small { font-size: 13px; }

.section-heading-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.import-box { margin-top: 14px; padding-top: 14px; border-top: 1px solid #e5e7eb; }
.import-box input[type="file"] { width: 100%; margin-bottom: 8px; }
.editor-panel label { margin-top: 12px; }
.script-list { display: grid; gap: 8px; }
.script-row { border: 1px solid #d1d5db; border-radius: 7px; padding: 10px; }
.script-row-title { font-weight: bold; overflow-wrap: anywhere; }
.script-row-meta { color: #6b7280; font-size: 12px; margin-top: 3px; }
.script-row-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 9px; }
.script-row-actions .button { padding: 8px 5px; min-height: 38px; font-size: 13px; }

.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.settings-grid label { margin: 0; }
.range-label { margin-top: 16px; }
.range-label input { display: block; margin-top: 4px; }
.check-row { margin: 14px 0; }
.check-row label { font-weight: normal; }
.check-row input { width: 20px; height: 20px; vertical-align: middle; margin-right: 5px; }

.device-list { display: grid; gap: 8px; }
.device-card { border: 1px solid #d1d5db; border-radius: 7px; padding: 10px; }
.device-card.offline { opacity: .65; }
.device-card-heading { display: -webkit-flex; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.device-name { font-weight: bold; min-width: 0; }
.device-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin-top: 9px; }
.device-actions .button { min-width: 0; padding-left: 7px; padding-right: 7px; font-size: 12px; }
.device-mirror-toggle { min-width: 0; }
.device-primary-button { min-width: 0; }
.device-primary-label { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 8px; border-radius: 7px; background: #dcfce7; color: #166534; font-weight: bold; font-size: 12px; }
.device-details { margin-top: 4px; color: #4b5563; font-size: 13px; line-height: 1.45; }
.receiver-summary-heading { margin-bottom: 4px; }

.alert { margin-bottom: 12px; padding: 10px 12px; border-radius: 7px; background: #dbeafe; color: #1e3a8a; }
.alert.error { background: #fee2e2; color: #991b1b; }
.alert.success { background: #dcfce7; color: #166534; }

.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; background: #111827; }
.login-card { width: 100%; max-width: 380px; padding: 22px; border-radius: 10px; background: #fff; }
.login-card h1 { margin-top: 0; }
.login-card label { margin-top: 14px; }
.login-card .button { margin-top: 16px; }

@media (max-width: 520px) {
    .tab-button { padding-left: 3px; padding-right: 3px; font-size: 12px; }
    .admin-main { padding: 8px; }
    .panel { padding: 11px; margin-bottom: 8px; }
    .button-grid.three { grid-template-columns: 1fr; }
    .settings-grid { grid-template-columns: 1fr; }
    .control-inline { flex-direction: column; }
    .control-inline > .button { width: 100%; }
}

.setting-help { margin: 4px 0 8px; }

.setting-subsection {
    margin: 18px 0;
    padding: 14px;
    border: 1px solid rgba(127, 127, 127, 0.28);
    border-radius: 8px;
}

.setting-subsection h3 {
    margin-top: 0;
}

/* Recording-friendly Scroll tab: intentionally oversized, low-precision controls. */
.scroll-remote-panel {
    padding: 10px;
}
.scroll-play-toggle {
    width: 100%;
    min-height: 260px;
    font-size: 34px;
    letter-spacing: .02em;
}
.scroll-speed-buttons {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 8px;
}
.scroll-speed-button {
    min-height: 230px;
    font-size: 24px;
}
.scroll-button-label,
.scroll-button-value {
    display: block;
}
.scroll-button-value {
    margin-top: 8px;
    font-size: 18px;
    font-weight: normal;
}

@media (max-height: 560px) {
    .scroll-play-toggle { min-height: 170px; }
    .scroll-speed-button { min-height: 140px; }
}

.drive-file-list { margin-top: 10px; display: grid; gap: 6px; max-height: 420px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.drive-file-row { display: -webkit-flex; display: flex; align-items: center; gap: 9px; padding: 9px; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; }
.drive-file-row.folder { cursor: pointer; }
.drive-file-row input[type="checkbox"] { width: 20px; height: 20px; flex: 0 0 auto; }
.drive-file-name { min-width: 0; flex: 1 1 auto; overflow-wrap: anywhere; }
.drive-file-type { flex: 0 0 auto; color: #6b7280; font-size: 12px; }

/* v2.0.2 floating admin feedback */
.admin-toast {
    position: fixed;
    top: 12px;
    left: 50%;
    z-index: 1000;
    width: calc(100% - 24px);
    max-width: 760px;
    transform: translateX(-50%);
    align-items: center;
    gap: 12px;
    padding: 12px 12px 12px 14px;
    border: 1px solid #93c5fd;
    border-radius: 9px;
    background: #dbeafe;
    color: #1e3a8a;
    box-shadow: 0 8px 24px rgba(0,0,0,.22);
}
.admin-toast.success { background: #dcfce7; border-color: #86efac; color: #166534; }
.admin-toast.error { background: #fee2e2; border-color: #fca5a5; color: #991b1b; }
.admin-toast.info { background: #dbeafe; border-color: #93c5fd; color: #1e3a8a; }
.admin-toast-text { min-width: 0; flex: 1 1 auto; font-weight: bold; line-height: 1.35; }
.admin-toast-close {
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: inherit;
    font-size: 28px;
    line-height: 34px;
    font-weight: normal;
}
.admin-toast-close:active { background: rgba(0,0,0,.08); }

/* Script actions stay in one row even on narrow phones. */
.script-row-actions { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.script-action-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    white-space: nowrap;
}
.script-action-icon {
    display: block;
    width: 16px;
    height: 16px;
    flex: 0 0 16px;
    fill: currentColor;
}

/* Google Drive stays connected but its browser is hidden until requested. */
.drive-browse-toggle { margin-top: 8px; }
.drive-browser-panel { margin-top: 8px; }
.drive-check-spacer { width: 20px; height: 20px; flex: 0 0 20px; }
.button:disabled {
    opacity: .58;
    cursor: default;
    transform: none;
}

@media (max-width: 520px) {
    .script-row-actions { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
    .script-row-actions .button { padding: 7px 2px; min-height: 42px; font-size: 11px; }
    .script-action-button { gap: 2px; }
    .script-action-icon { width: 14px; height: 14px; flex-basis: 14px; }
}
.drive-footer-controls {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
}
.drive-footer-controls > .button { width: auto; min-width: 0; }

/* v2.1.0 producer Manual Positioning */
.scroll-tab-panel { padding-bottom: 78px; }
.manual-position-launch {
    position: fixed;
    left: 50%;
    bottom: 14px;
    z-index: 35;
    width: calc(100% - 28px);
    max-width: 792px;
    transform: translateX(-50%);
    text-align: center;
    text-decoration: none;
    background: #fff;
    box-shadow: 0 4px 16px rgba(0,0,0,.14);
}
.manual-position-launch:active {
    /* Preserve horizontal centering; the generic .button:active transform otherwise
       replaces translateX(-50%) and makes this fixed link jump to the right. */
    transform: translateX(-50%) translateY(1px);
}

.manual-position-body {
    overflow: hidden;
    background: #1f2937;
}

.manual-position-shell {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #f3f4f6;
}
.manual-position-header {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    min-height: 58px;
    padding: 8px 10px;
    background: #111827;
    color: #fff;
}
.manual-back-link {
    color: #dbeafe;
    font-weight: bold;
    text-decoration: none;
    white-space: nowrap;
}
.manual-header-center { min-width: 0; text-align: center; }
.manual-script-title {
    margin-top: 2px;
    color: #cbd5e1;
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.manual-position-controls {
    flex: 0 0 auto;
    padding: 8px 10px;
    background: #fff;
    border-bottom: 1px solid #d1d5db;
}
.manual-play-toggle {
    width: 100%;
    min-height: 144px;
    font-size: 26px;
}
.manual-position-instruction {
    margin-top: 7px;
    color: #4b5563;
    font-size: 12px;
    text-align: center;
}
.manual-script-viewport {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: #000;
    color: #fff;
    overscroll-behavior: contain;
}
.manual-script-content {
    box-sizing: border-box;
    margin: 0 auto;
    color: #fff;
    /* The wrapper contains formatting whitespace around #manualScriptText in the
       HTML template. Preserving that whitespace creates anonymous line boxes before
       the actual script on Android. The child #manualScriptText controls script
       whitespace explicitly, so the wrapper itself must not preserve indentation. */
    white-space: normal;
    overflow-wrap: normal;
}
.manual-script-text { position: relative; }
.manual-position-footer {
    flex: 0 0 auto;
    margin-top: auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    min-height: 48px;
    padding: 8px 10px;
    background: #fff;
    border-top: 1px solid #d1d5db;
}
.manual-position-footer > :last-child { text-align: right; }
.manual-receiver-warning { color: #b45309; font-weight: bold; }

.manual-position-footer > .manual-geometry-debug { text-align: left; }

.manual-geometry-debug {
    display: none;
    flex: 1 0 100%;
    width: 100%;
    margin-top: 4px;
    padding-top: 4px;
    border-top: 1px dashed #d1d5db;
    color: #374151;
    font: 9px/1.25 monospace;
    text-align: left;
    white-space: pre-wrap;
    word-break: break-word;
}

@media (max-width: 420px) {
    .manual-position-header { grid-template-columns: auto minmax(0, 1fr); }
    .manual-position-header .status-badge { display: none; }
    .manual-play-toggle { min-height: 128px; font-size: 23px; }
    .manual-position-footer { font-size: 12px; }
}

/* v2.2.0 Primary-receiver Manual Positioning preview */
.manual-preview-host {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;
    padding: 0;
    box-sizing: border-box;
    background: #111827;
}
.manual-receiver-frame {
    position: relative;
    flex: 0 0 auto;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    overflow: hidden;
    box-sizing: border-box;
    background: #000;
    outline: 1px solid rgba(255,255,255,0.18);
    box-shadow: 0 2px 12px rgba(0,0,0,0.28);
}
.manual-preview-host .manual-script-viewport {
    position: absolute;
    z-index: 10;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 0;
    box-sizing: border-box;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.manual-preview-scroll-surface {
    position: relative;
    width: 100%;
    min-height: 100%;
    box-sizing: border-box;
    /* This is the authoritative scaled Primary document extent. The visual layer
       is rendered in the same Admin-pixel coordinate system and cannot enlarge it. */
    overflow: hidden;
}
.manual-receiver-scale-layer {
    position: absolute;
    top: 0;
    left: 0;
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
}
.manual-preview-line {
    display: block;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0;
    white-space: pre;
    overflow: visible;
}
.manual-preview-focus-band {
    display: none;
    position: absolute;
    z-index: 5;
    left: 0;
    width: 100%;
    pointer-events: none;
    box-sizing: border-box;
}
.manual-preview-waiting {
    position: absolute;
    z-index: 30;
    left: 12px;
    right: 12px;
    top: 50%;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
    padding: 12px;
    border-radius: 6px;
    background: rgba(17,24,39,0.90);
    color: #fff;
    text-align: center;
    font-size: 13px;
    pointer-events: none;
}
.manual-footer-status { min-width: 0; text-align: right; }
.manual-footer-status .tiny { margin-top: 2px; font-size: 10px; }
.manual-script-content {
    -webkit-text-size-adjust: none;
    text-size-adjust: none;
}

@media (orientation: landscape) and (max-height: 600px) {
    .manual-position-header {
        min-height: 42px;
        padding: 4px 8px;
    }
    .manual-script-title { font-size: 10px; }
    .manual-position-controls { padding: 4px 8px; }
    .manual-play-toggle {
        min-height: 92px;
        font-size: 21px;
    }
    .manual-position-instruction {
        margin-top: 3px;
        font-size: 10px;
    }
    .manual-position-footer {
        min-height: 34px;
        padding: 4px 8px;
    }
    .manual-preview-host { padding: 0; }
}

/* v3.0 production controls */
input[type="range"] {
    touch-action: pan-y;
}
.anchor-button-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.anchor-button-list .button {
    flex: 1 1 auto;
    min-width: 120px;
}
.gate-control-panel {
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid rgba(255,255,255,0.18);
}
.gate-status {
    margin-bottom: 8px;
    font-weight: 700;
    text-align: center;
}
.gate-continue-button {
    min-height: 64px;
    font-size: 1.08rem;
}
.device-name strong {
    white-space: nowrap;
}

/* Primary-authoritative Manual Positioning block geometry. */
.manual-script-text .tp-line {
    display: block;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    word-wrap: break-word;
}
.manual-script-text .tp-blank-line { min-height: 1em; }
.manual-script-text .tp-host-only { color: #00E5FF; }
.manual-script-text .tp-stage-direction { color: #FFD54F; }
.manual-script-text .tp-gate { color: #FF7A00; font-weight: 700; }
.manual-script-text .tp-anchor { display: inline; width: 0; height: 0; }

.manual-primary-line {
    display: block;
    overflow: visible;
    white-space: pre;
    overflow-wrap: normal;
    word-wrap: normal;
}
