/* ═══════════════════════════════════════════════════════════
   Shape Optimizer — Layout & visual style
   ═══════════════════════════════════════════════════════════ */

:root {
    --col-bg:        #f4f6f9;
    --col-panel:     #ffffff;
    --col-border:    #d8dee6;
    --col-text:      #222;
    --col-muted:     #666;
    --col-primary:   #2196F3;
    --col-primary-dark: #1976D2;
    --col-success:   #2E7D32;
    --col-success-bg:#E8F5E9;
    --col-danger:    #C62828;
    --col-danger-bg: #FFEBEE;
    --col-record:    #FF6F00;
    --col-record-bg: #FFF8E1;
    --col-start:     #4CAF50;
    --shadow:        0 1px 3px rgba(0,0,0,.1);
}

* { box-sizing: border-box; }
html, body {
    margin: 0; padding: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    color: var(--col-text);
    background: var(--col-bg);
    height: 100%;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

#app {
    display: grid;
    grid-template-columns: 280px 1fr 320px;
    grid-template-areas: "sidebar canvas score";
    height: 100vh;
    width: 100vw;
}

#sidebar       { grid-area: sidebar; }
#canvas-area   { grid-area: canvas;  }
#score-panel   { grid-area: score;   }

/* ───── Sidebar ──────────────────────────────────────── */
#sidebar {
    background: var(--col-panel);
    border-right: 1px solid var(--col-border);
    overflow-y: auto;
    padding: 12px;
}
#sidebar h1 {
    font-size: 18px;
    margin: 0 0 12px;
    color: var(--col-primary-dark);
}
#sidebar h2 {
    font-size: 14px;
    margin: 0 0 8px;
    color: var(--col-muted);
    text-transform: uppercase;
    letter-spacing: .05em;
}
#logo-link {
    display: block;
    margin: 0 auto 12px;
    line-height: 0;
    text-align: center;
}
#logo-img {
    display: block;
    width: 100%;
    max-width: 200px;
    height: auto;
    margin: 0 auto;
    border-radius: 6px;
    transition: opacity .15s;
}
#logo-img:hover { opacity: .85; }

.panel {
    margin-bottom: 14px;
}
fieldset {
    border: 1px solid var(--col-border);
    border-radius: 6px;
    padding: 8px 10px;
    margin: 0 0 10px;
}
fieldset legend {
    font-size: 12px;
    color: var(--col-muted);
    padding: 0 4px;
}
fieldset label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    margin: 4px 0;
}
fieldset label input[type="text"],
fieldset label input[type="number"],
fieldset label select {
    flex: 1;
    padding: 4px 6px;
    border: 1px solid var(--col-border);
    border-radius: 4px;
    font: inherit;
    min-width: 0;
}
.radio-row {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 10px;
}
.radio-row label {
    font-size: 13px;
    cursor: pointer;
}
.btn-row {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}
.btn-row button { flex: 1; min-width: 80px; }

.hint {
    font-size: 11px;
    color: var(--col-muted);
    margin: 4px 0 0;
}

/* Buttons */
button {
    padding: 8px 10px;
    border-radius: 6px;
    border: 1px solid var(--col-border);
    background: #f6f8fa;
    cursor: pointer;
    font: inherit;
    color: var(--col-text);
    transition: filter 0.1s;
    width: 100%;
    margin-top: 6px;
}
button:hover { filter: brightness(0.96); }
button:active { filter: brightness(0.9); }
button.btn-primary {
    background: var(--col-primary);
    border-color: var(--col-primary-dark);
    color: white;
    font-weight: bold;
}
button.btn-secondary { /* default */ }
button#btn-start {
    background: var(--col-start);
    border-color: #388E3C;
    color: white;
    font-weight: bold;
    font-size: 15px;
    padding: 10px;
    margin-top: 8px;
}
button.btn-big {
    font-size: 18px;
    padding: 14px;
    margin-top: 8px;
}

/* ───── Canvas area ──────────────────────────────────── */
#canvas-area {
    position: relative;
    background: #fafbfc;
    overflow: hidden;
    touch-action: none;             /* prevent browser pan/zoom on touch */
}
#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}
#zoom-controls {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    z-index: 5;
}
#zoom-controls button {
    width: 40px;
    height: 40px;
    margin: 0;
    padding: 0;
    border-radius: 6px;
    border: 1px solid var(--col-border);
    background: rgba(255,255,255,0.92);
    box-shadow: var(--shadow);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    user-select: none;
}
#zoom-controls button:hover { background: white; }

/* ───── Score panel ──────────────────────────────────── */
#score-panel {
    background: var(--col-panel);
    border-left: 1px solid var(--col-border);
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
#score-card {
    border: 3px solid var(--col-border);
    border-radius: 10px;
    padding: 16px 12px;
    text-align: center;
    background: white;
    transition: background 0.2s, border-color 0.2s;
}
#score-card.state-record {
    border-color: var(--col-record);
    background: var(--col-record-bg);
    box-shadow: 0 0 12px rgba(255, 111, 0, .25);
}
#score-card.state-pass    { border-color: var(--col-success); background: var(--col-success-bg); }
#score-card.state-zero    { border-color: var(--col-primary); }
#score-card.state-over    { border-color: var(--col-danger);  background: var(--col-danger-bg);  }

#score-banner {
    font-size: 13px;
    font-weight: bold;
    margin-bottom: 6px;
    color: var(--col-muted);
}
#score-number {
    font-size: 64px;
    font-weight: bold;
    line-height: 1;
    color: var(--col-muted);
    transition: color 0.2s;
}
#score-card.state-record #score-number { color: var(--col-record); }
#score-card.state-pass   #score-number { color: var(--col-success); }
#score-card.state-zero   #score-number { color: var(--col-primary); }
#score-card.state-over   #score-number { color: var(--col-danger); }

#score-sub {
    font-size: 12px;
    color: #444;
    margin-top: 6px;
}

#stress-card {
    border: 2px solid var(--col-border);
    border-radius: 8px;
    padding: 10px 12px;
    background: white;
}
#stress-card.state-ok   { border-color: var(--col-success); }
#stress-card.state-over { border-color: var(--col-danger);  }
#stress-label  { font-size: 12px; color: var(--col-muted); }
#stress-values { font-size: 18px; font-weight: bold; margin: 4px 0; color: #444; }
#stress-card.state-ok   #stress-values,
#stress-card.state-ok   #stress-status { color: var(--col-success); }
#stress-card.state-over #stress-values,
#stress-card.state-over #stress-status { color: var(--col-danger);  }
#stress-status { font-weight: bold; font-size: 14px; }
#stress-bar {
    position: relative;
    width: 100%;
    height: 18px;
    border: 1px solid #888;
    border-radius: 3px;
    margin: 6px 0 2px;
    /* jet-style gradient */
    background: linear-gradient(to right,
        rgb(0,0,255),    rgb(0,127,255),
        rgb(0,255,255),  rgb(127,255,127), rgb(255,255,0),
        rgb(255,127,0),  rgb(255,0,0),  rgb(127,0,0));
}
#stress-bar-marker {
    position: absolute;
    top: -3px;
    width: 2px;
    height: 24px;
    background: #000;
    box-shadow: 0 0 0 1px white;
    transform: translateX(-1px);
    display: none;
    transition: left .2s;
}
#stress-bar-labels {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--col-muted);
    font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
    margin-bottom: 2px;
}

#time-card {
    border: 2px solid var(--col-border);
    border-radius: 8px;
    padding: 8px 12px;
    background: white;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
}
#time-label { font-size: 12px; color: var(--col-muted); }
#time-value {
    font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
    font-size: 22px; font-weight: bold; color: #444;
}

#history-card {
    border: 1px solid var(--col-border);
    border-radius: 8px;
    padding: 8px;
    background: white;
    flex: 1;
    min-height: 160px;
    display: flex;
    flex-direction: column;
}
.history-title {
    font-size: 12px;
    color: var(--col-muted);
    margin-bottom: 4px;
}
#history-canvas {
    flex: 1;
    width: 100%;
    height: 100%;
}

/* ═══════════════════════════════════════════════════════════
   Mobile / narrow screens
   ─ Setup phase: scrollable (parameters need full input)
   ─ Play phase:  single-screen (compact stats + canvas + actions)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 900px) {
    html, body { overflow: auto; }
    #app {
        grid-template-columns: 1fr;
        grid-template-areas:
            "score"
            "canvas"
            "sidebar";
        height: auto;
        min-height: 100vh;
    }
    #sidebar     { border-right: none; border-top: 1px solid var(--col-border); max-height: none; }
    #score-panel { border-left:  none; border-bottom: 1px solid var(--col-border); flex-direction: row; flex-wrap: wrap; }
    #score-card, #stress-card, #time-card { flex: 1 1 220px; }
    #history-card { flex: 1 1 100%; min-height: 140px; }
    #canvas-area { height: 60vh; }
    button { padding: 12px; font-size: 15px; }       /* larger tap targets */
    fieldset label { font-size: 14px; }
    fieldset label input { padding: 8px; font-size: 15px; }
}

/* Play phase on mobile: lock to viewport, no scroll, compact bars */
@media (max-width: 900px) {
    body.phase-play, body.phase-play html { overflow: hidden; }
    body.phase-play #app {
        grid-template-rows: auto 1fr auto;
        grid-template-areas:
            "score"
            "canvas"
            "sidebar";
        /* dvh = dynamic viewport height (excludes mobile browser chrome) */
        height: 100vh;            /* fallback */
        height: 100dvh;
        min-height: 0;
        /* respect notches / status bar */
        padding-top:    env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        padding-left:   env(safe-area-inset-left);
        padding-right:  env(safe-area-inset-right);
        box-sizing: border-box;
    }
    /* Top compact stat bar */
    body.phase-play #score-panel {
        padding: 6px;
        gap: 6px;
        max-height: 30dvh;
        overflow: hidden;
        flex-shrink: 0;
    }
    body.phase-play #canvas-area { min-height: 0; }
    body.phase-play #score-card {
        padding: 6px 10px;
        flex: 1 1 110px;
        border-width: 2px;
    }
    body.phase-play #score-banner { font-size: 11px; margin-bottom: 2px; }
    body.phase-play #score-number { font-size: 28px; }
    body.phase-play #score-sub    { font-size: 10px; margin-top: 2px; }

    body.phase-play #time-card    { flex: 0 0 90px; padding: 6px 10px; flex-direction: column; align-items: flex-start; }
    body.phase-play #time-value   { font-size: 18px; }

    body.phase-play #stress-card  { flex: 1 1 100%; padding: 6px 10px; }
    body.phase-play #stress-values{ font-size: 14px; margin: 2px 0; }
    body.phase-play #stress-status{ font-size: 12px; }
    body.phase-play #stress-bar   { height: 14px; margin: 4px 0 2px; }
    body.phase-play #stress-bar-marker { height: 20px; }

    body.phase-play #history-card { display: none; }   /* hide graph for space */

    /* Canvas grows to remaining */
    body.phase-play #canvas-area { height: auto; }

    /* Bottom compact action bar (Play panel) */
    body.phase-play #sidebar {
        padding: 6px;
        max-height: 26dvh;
        overflow-y: auto;
        flex-shrink: 0;
    }
    body.phase-play #sidebar h1 { display: none; }
    body.phase-play #panel-play h2 { display: none; }
    body.phase-play #logo-link {
        margin: 0 auto 6px;
        text-align: center;
    }
    body.phase-play #logo-img {
        max-width: 160px;
        margin: 0 auto;
    }
    body.phase-play #panel-play fieldset { padding: 4px 8px; margin-bottom: 6px; }
    body.phase-play #panel-play button { padding: 8px; font-size: 14px; margin-top: 4px; }
    body.phase-play #panel-play .btn-big { padding: 10px; font-size: 15px; }
}

@media (max-width: 500px) {
    #score-number { font-size: 48px; }
    #score-panel { gap: 8px; padding: 8px; }
    #sidebar { padding: 8px; }
    body.phase-play #score-number { font-size: 24px; }
}
