/* CSS Variables for Dark Theme */
:root {
    --bg-color: #050505;
    --text-color: #ff3333; /* 劇場等の暗所でも眩しくない赤色を採用 */
    --text-color-active: #ff5555;
    --glow-color: rgba(255, 0, 0, 0.4);
    --control-bg: rgba(30, 30, 30, 0.85);
    --border-color: #333;
    --btn-primary: #880000;
    --btn-primary-hover: #aa0000;
    --btn-secondary: #2a2a2a;
    --btn-secondary-hover: #3d3d3d;
}

/* Base Normalize */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    touch-action: manipulation;
    -webkit-user-select: none;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    width: 100vw;
    overflow-x: hidden;
    overflow-y: auto;
    display: flex;
    justify-content: center;
    align-items: center;
}

#app {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 100%;
    height: 100%;
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 24px;
    background-color: var(--control-bg);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.control-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

label {
    font-size: 1.1rem;
    color: #ccc;
    font-weight: 500;
}

select {
    background-color: #1a1a1a;
    color: #fff;
    border: 1px solid #444;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 1.1rem;
    outline: none;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
}

select:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.icon-btn {
    background: none;
    border: none;
    color: #ccc;
    font-size: 1.8rem;
    cursor: pointer;
    padding: 5px;
    transition: color 0.2s;
}

.icon-btn:hover {
    color: #fff;
}

.text-toggle-btn {
    background-color: #2a2a2a;
    color: #ccc;
    border: 1px solid #444;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
}

#btn-audio-sync {
    min-width: 140px;
}

.text-toggle-btn.active {
    background-color: #ff3333;
    color: #fff;
    border-color: #ff3333;
    box-shadow: 0 0 10px rgba(255, 51, 51, 0.5);
}

/* Tabs Navigation */
.tabs {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 15px 0;
}

.tab-btn {
    background-color: transparent;
    color: #888;
    border: 1px solid #444;
    border-radius: 20px;
    padding: 8px 24px;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    letter-spacing: 1px;
    min-width: 160px;
    text-align: center;
}

.tab-btn:hover {
    color: #fff;
    border-color: #666;
}

.tab-btn.active {
    background-color: var(--btn-primary);
    color: #fff;
    border-color: var(--btn-primary);
    box-shadow: 0 0 10px rgba(136, 0, 0, 0.5);
}

/* Main Display */
main {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    width: 100%;
}

.visual-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 20px;
}

.recv-container {
    width: 95%;
    max-width: 600px;
    margin-top: 10px;
    background-color: #151515;
    border: 1px solid #333;
    border-radius: 8px;
    padding: 10px 15px;
}

.recv-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.recv-row label {
    color: #888;
    font-size: 0.85rem;
    white-space: nowrap;
    min-width: 110px;
    text-align: left;
}

#audio-input-select {
    flex-grow: 1;
    max-width: none;
    font-size: 0.85rem;
    padding: 6px 10px;
}

.status-badge {
    padding: 4px 10px;
    border-radius: 6px;
    font-weight: bold;
    font-size: 0.85rem;
    letter-spacing: 1px;
}

.status-badge.error {
    background-color: #3a1111;
    color: #ff5555;
    border: 1px solid #ff3333;
}

.status-badge.sync {
    background-color: #113a11;
    color: #55ff55;
    border: 1px solid #33ff33;
}

.tab-content {
    display: none;
    width: 100%;
    text-align: center;
    justify-content: center;
    align-items: center;
}

.tab-content.active {
    display: flex;
    animation: fadeIn 0.3s ease-in-out;
}

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

#timecode-display {
    position: relative;
    font-family: 'Share Tech Mono', monospace;
    font-size: clamp(3rem, 14vw, 250px);
    font-variant-numeric: tabular-nums;
    text-shadow: 0 0 15px var(--glow-color);
    line-height: 1;
    display: flex;
    gap: 1vw;
    white-space: nowrap;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.3s ease;
}

/* ゲインオーバー(CLIP) 表示用インジケーター */
.clip-indicator {
    position: absolute;
    bottom: 5px;
    right: 15px;
    font-family: 'Inter', sans-serif;
    font-size: clamp(0.5rem, 2vw, 1.2rem);
    font-weight: 800;
    color: #ff3333;
    opacity: 0;
    transition: opacity 0.1s ease;
    pointer-events: none;
    z-index: 100;
    text-shadow: 0 0 10px rgba(255, 51, 51, 0.8);
    background: rgba(0, 0, 0, 0.4);
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid rgba(255, 51, 51, 0.5);
    line-height: 1;
}

.clip-indicator.is-clipping {
    opacity: 1;
}

span[id^="tc-"] {
    display: inline-block;
    min-width: 2ch;
    text-align: center;
}

/* LTC Panel */
.ltc-panel {
    background-color: var(--control-bg);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 30px;
    max-width: 600px;
    margin: 0 auto;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}

.ltc-panel h3 {
    color: #fff;
    margin-bottom: 15px;
    font-size: 1.5rem;
    letter-spacing: 1px;
}

.ltc-desc {
    color: #aaa;
    line-height: 1.6;
    margin-bottom: 25px;
    font-size: 1.1rem;
}

.ltc-status {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    background-color: #111;
    padding: 10px 24px;
    border-radius: 30px;
    border: 1px solid #333;
}

.indicator {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background-color: #555;
    box-shadow: 0 0 5px #000;
    transition: all 0.3s;
}

.indicator.active {
    background-color: #ff3333;
    box-shadow: 0 0 15px #ff3333;
    animation: pulse 2s infinite;
}

#ltc-status-text {
    font-weight: bold;
    color: #aaa;
    min-width: 130px;
    letter-spacing: 2px;
}

#ltc-status-text.active {
    color: #ff3333;
}

@keyframes pulse {
    0% { box-shadow: 0 0 10px rgba(255, 51, 51, 0.4); }
    50% { box-shadow: 0 0 20px rgba(255, 51, 51, 0.8); }
    100% { box-shadow: 0 0 10px rgba(255, 51, 51, 0.4); }
}

/* LTC Panel enhancements */
.ltc-section {
    margin: 20px 0;
}

.ltc-section h4 {
    color: #ccc;
    margin-bottom: 12px;
    font-size: 1.1rem;
    font-weight: normal;
}

.ltc-divider {
    border: 0;
    height: 1px;
    background: #444;
    margin: 25px 0;
}

.export-form {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    align-items: center;
    justify-content: center;
}

.form-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tc-input, .dur-input {
    background-color: #1a1a1a;
    color: #fff;
    border: 1px solid #444;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 1.1rem;
    outline: none;
    font-family: 'Share Tech Mono', monospace;
}

.tc-input { width: 140px; text-align: center; }
.dur-input { width: 80px; text-align: right; }

.small-btn {
    padding: 10px 20px;
    font-size: 1rem;
    margin-left: 10px;
}

/* Help Panel */
.help-panel {
    background-color: var(--control-bg);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 30px;
    max-width: 800px;
    margin: 0 auto;
    text-align: left;
    max-height: 60vh;
    overflow-y: auto;
}

.help-panel h2 {
    color: #fff;
    margin-bottom: 15px;
    border-bottom: 1px solid #444;
    padding-bottom: 10px;
}

.help-intro {
    color: #ccc;
    line-height: 1.6;
    margin-bottom: 25px;
    font-size: 0.95rem;
}

.help-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
}

.help-card {
    background-color: #111;
    border: 1px solid #333;
    padding: 20px;
    border-radius: 12px;
}

.help-card h3 {
    color: #ff5555;
    margin-bottom: 10px;
    font-size: 1.1rem;
}

.help-card p {
    color: #bbb;
    line-height: 1.5;
    font-size: 0.9rem;
}

.help-footer {
    margin-top: 25px;
    padding-top: 15px;
    border-top: 1px solid #444;
    color: #888;
    font-size: 0.85rem;
}

/* Footer Controls */
footer {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    padding: 20px;
    background-color: var(--control-bg);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -5px 15px rgba(0,0,0,0.5);
    z-index: 10;
    flex-wrap: wrap;
}

.tc-manual-group {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-right: 15px;
}

.tc-manual-group label {
    color: #ccc;
    font-size: 0.95rem;
}

button {
    font-size: 1.3rem;
    font-weight: bold;
    padding: 18px 45px;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.2s ease;
    text-transform: uppercase;
    letter-spacing: 2px;
}

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

.primary-btn {
    background-color: var(--btn-primary);
    color: #fff;
    box-shadow: 0 4px 15px rgba(136, 0, 0, 0.4);
}

.primary-btn:not(:disabled):hover {
    background-color: var(--btn-primary-hover);
    transform: scale(1.05);
}

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

.secondary-btn:not(:disabled):hover {
    background-color: var(--btn-secondary-hover);
    color: #fff;
    transform: scale(1.02);
}

/* Mic Connect Button States */
.btn-disconnected {
    background-color: #aa0000 !important;
    color: #fff !important;
    border: 1px solid #ff3333 !important;
    box-shadow: 0 0 10px rgba(255,51,51,0.4);
}

.btn-connected {
    background-color: #0044aa !important;
    color: #fff !important;
    border: 1px solid #3388ff !important;
    box-shadow: 0 0 10px rgba(51,136,255,0.4);
}

/* Responsive (Mobile Portrait) */
@media (orientation: portrait) {
    #timecode-display {
        font-size: clamp(2.5rem, 14vw, 250px);
        flex-direction: row;
        align-items: center;
        justify-content: center;
        gap: 0;
        text-shadow: 0 0 10px var(--glow-color);
    }

    footer {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 10px;
        padding: 10px;
    }
    
    .tc-manual-group {
        width: 100%;
        justify-content: center;
        margin-right: 0;
        margin-bottom: 5px;
    }

    button {
        flex: 1;
        width: auto;
        padding: 15px 10px;
        font-size: 1.1rem;
    }
}

/* Responsive (Small phones / iPhone compact mode) */
@media (max-width: 500px) {
    .tab-btn {
        padding: 6px 14px;
        font-size: 0.85rem;
    }
    
    header label, header select, .text-toggle-btn {
        font-size: 0.85rem;
        white-space: nowrap;
    }
    
    .text-toggle-btn {
        padding: 4px 8px;
    }
    
    /* 狭い画面ではLabelとInputを縦並びにし、横幅をフル確保 */
    .tc-manual-group {
        flex-direction: column;
        align-items: center;
        gap: 5px;
    }
    .tc-manual-group label {
        font-size: 0.85rem;
    }
    .tc-input {
        font-size: 1rem;
        padding: 10px;
        width: 100%;
        max-width: 250px;
    }
    
    /* LTC受信デバイスなどの折り返しや表示 */
    .recv-row {
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }
    #audio-input-select {
        max-width: 100%;
        width: 100%;
        font-size: 0.8rem;
    }
    
    button {
        padding: 14px 6px;
        font-size: 0.95rem;
    }
    
    /* ヘッダー全体も少し詰める */
    header {
        padding: 8px 10px;
        flex-wrap: wrap;
        gap: 8px;
        justify-content: center;
    }
    .control-group {
        width: auto;
    }
    .tabs {
        margin: 8px 0;
    }
}

/* Landscape Phone Heights - 超コンパクトモード */
@media (max-height: 500px) and (orientation: landscape) {
    #app { padding: 5px 10px; justify-content: space-between; }
    header { padding: 5px 10px; }
    .tabs { margin: 2px 0; }
    .tab-btn { padding: 4px 15px; font-size: 0.85rem; }
    #timecode-display { font-size: clamp(2rem, 15vw, 150px); margin: -10px 0; }
    footer { padding: 5px 10px; gap: 8px; }
    button { padding: 8px 16px; font-size: 1rem; }
    .tc-manual-group { margin-right: 5px; }
    .ltc-panel, .recv-container { padding: 10px; margin-top: 5px; }
    .ltc-panel h3 { margin-bottom: 5px; font-size: 1.2rem; }
    .ltc-desc { margin-bottom: 10px; font-size: 0.9rem; }
}

/* ==========================================
   Language Toggle System (EN / JP)
========================================== */
body.lang-jp .lang-en { display: none !important; }
body.lang-en .lang-jp { display: none !important; }

/* In-line elements that need to remain inline */
body.lang-jp span.lang-jp { display: inline; }
body.lang-en span.lang-en { display: inline; }

/* ==========================================
   Color Palette & Minimal Mode
========================================== */
.color-palette {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-top: 5px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    padding: 0 10px;
}

.color-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.2s, border-color 0.2s;
    box-shadow: 0 0 5px rgba(0,0,0,0.5);
    background-clip: padding-box;
}

.color-btn:hover {
    transform: scale(1.2);
}

.color-btn.active {
    border-color: #fff;
    transform: scale(1.2);
    box-shadow: 0 0 10px currentColor;
}

header, footer, .tabs, .recv-container, .color-palette, .ltc-panel, .help-panel {
    transition: opacity 0.3s ease;
}

/* Minimal Mode (Hide controls on tap but keep layout exactly identical) */
body.minimal-mode header,
body.minimal-mode footer,
body.minimal-mode .tabs,
body.minimal-mode .recv-container,
body.minimal-mode .color-palette,
body.minimal-mode .ltc-panel,
body.minimal-mode .help-panel {
    opacity: 0 !important;
    pointer-events: none !important;
}

@media (max-width: 480px) {
    .color-palette { gap: 8px; margin-bottom: 10px; }
    .color-btn { width: 32px; height: 32px; }
}

/* ==========================================
   TX/RX Mode Separation Styling
========================================== */
.mode-toggle-wrapper {
    display: flex;
    justify-content: center;
    margin: 15px 0 5px 0;
    padding: 0 20px;
    transition: opacity 0.3s ease;
}

body.minimal-mode .mode-toggle-wrapper {
    opacity: 0 !important;
    pointer-events: none !important;
}

.mode-toggle {
    display: flex;
    background: var(--control-bg);
    border-radius: 30px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 10px rgba(0,0,0,0.5);
}

.mode-toggle input {
    display: none;
}

.mode-toggle label {
    padding: 12px 30px;
    cursor: pointer;
    font-size: 1.1rem;
    font-weight: bold;
    color: #888;
    transition: all 0.3s ease;
    text-align: center;
    min-width: 180px;
}

.mode-toggle input:checked + label {
    background: var(--btn-primary);
    color: #fff;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.3);
}

.mode-toggle input:disabled + label {
    opacity: 0.3;
    cursor: not-allowed;
    background-color: #222;
    color: #555;
    pointer-events: none;
}

/* -- Visibility Rules Based on Mode -- */

/* In RX (Reader) Mode: Hide Sender elements */
body[data-mode="rx"] footer {
    display: none !important;
}
body[data-mode="rx"] .tabs [data-tab="ltc"] {
    display: none !important;
}
body[data-mode="rx"] #btn-audio-sync {
    display: none !important;
}

/* In TX (Sender) Mode: Hide Receiver elements */
body[data-mode="tx"] .recv-container {
    display: none !important;
}

/* -----------------------------
 * Bounding Box UI (Only visibly in minimal mode)
 * ----------------------------- */
.resize-handle {
    display: none;
    position: absolute;
    width: 16px;
    height: 16px;
    background-color: rgba(255, 255, 255, 0.25);
    border-radius: 50%;
    z-index: 10001;
    touch-action: none;
    transition: background-color 0.2s, transform 0.2s;
}

.resize-handle:hover, .resize-handle:active {
    background-color: rgba(255, 255, 255, 0.8);
    transform: scale(1.2);
}

.resize-close-btn {
    display: none;
    position: absolute;
    top: 0px;
    right: 0px;
    width: 16px;
    height: 16px;
    background-color: transparent;
    color: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    font-size: 1.1rem;
    font-weight: 300; /* Sleek thin font */
    font-family: sans-serif;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10002;
    touch-action: manipulation;
    transition: color 0.2s, transform 0.2s, background-color 0.2s;
}

.resize-close-btn:hover {
    color: rgba(255, 255, 255, 0.9);
    background-color: rgba(200, 50, 50, 0.4); /* subtle red highlight */
}

body.minimal-mode #timecode-display {
    /* No visible border, just the handles */
    border: none;
    padding: 10px 20px;
    box-sizing: border-box;
    position: relative; 
}

body.minimal-mode .resize-handle {
    display: block;
}
body.minimal-mode .resize-close-btn {
    display: flex;
}

/* Positioning handles tightly near the text boundaries */
.top-left     { top: 0px; left: 0px; cursor: nwse-resize; }
.bottom-left  { bottom: 0px; left: 0px; cursor: nesw-resize; }
.bottom-right { bottom: 0px; right: 0px; cursor: nwse-resize; }

.resize-close-btn:active {
    transform: scale(0.9);
}

@media (max-width: 500px) {
    .mode-toggle label {
        padding: 10px 15px;
        font-size: 0.95rem;
        min-width: 130px;
    }
}

/* -----------------------------
 * Alert State UI
 * ----------------------------- */
#timecode-display.signal-lost {
    color: #222 !important; /* Extremely dark gray, almost black */
    transition: color 0.3s ease;
}

/* -----------------------------
 * Setup Guide / Affiliate Links
 * ----------------------------- */
.setup-guide-card {
    grid-column: 1 / -1; /* Make it span all columns */
    border: 1px solid var(--accent);
    background: rgba(0, 255, 0, 0.02);
}

.setup-section {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px dashed rgba(255, 255, 255, 0.1);
}

.setup-section:last-of-type {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.setup-section h4 {
    font-size: 1rem;
    color: var(--highlight);
    margin-bottom: 10px;
}

.setup-steps {
    padding-left: 20px;
    margin: 10px 0;
    line-height: 1.6;
    font-size: 0.9rem;
}

.setup-steps li {
    margin-bottom: 8px;
}

.warning-list {
    list-style: none;
    padding-left: 0;
}

.warning-list li {
    margin-bottom: 8px;
    padding-left: 20px;
    position: relative;
    line-height: 1.5;
    font-size: 0.9rem;
}

.warning-list li::before {
    content: "⚠️";
    position: absolute;
    left: 0;
    font-size: 0.9em;
}

.gear-list {
    list-style: none;
    padding: 0;
    margin: 10px 0;
}

.gear-list li {
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px dotted rgba(255, 255, 255, 0.05);
}

.gear-list li:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.gear-list strong {
    display: block;
    font-size: 1.05rem;
    color: var(--highlight);
    margin-bottom: 5px;
}

.gear-links {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.affiliate-link {
    display: inline-flex;
    align-items: center;
    padding: 8px 15px;
    background: linear-gradient(180deg, #fad961 0%, #f76b1c 100%);
    color: #000;
    text-decoration: none;
    border-radius: 6px;
    font-weight: bold;
    font-size: 0.9rem;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.affiliate-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(247, 107, 28, 0.4);
}

.gear-note {
    font-size: 0.8rem;
    color: #888;
    margin-top: 20px;
    text-align: center;
    line-height: 1.4;
}
