:root {
    --button-size: clamp(34px, calc((100vw - 40px) / 7), 40px);
    font-family: system-ui, -apple-system, "Noto Sans JP", sans-serif;
    color: #14213d;
    background: #f6f4ef;
    font-size: 15px;
    font-weight: 900;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

button,
input {
    font: inherit;
    color: inherit;
}

button {
    border: 2px solid #aab2c0;
    background: white;
    border-radius: 10px;
    min-height: 42px;
    padding: 6px 10px;
    cursor: pointer;
}

button.tap-pending {
    border-color: #04b3e8 !important;
    box-shadow: 0 0 0 3px rgba(4, 133, 232, 0.35);
    filter: brightness(1.12);
}

button:focus-visible,
input:focus-visible {
    outline: 3px solid #0472e8;
    outline-offset: 2px;
}


/* =========================
   アプリ全体
========================= */

.app {
    height: 100%;
    max-width: 1100px;
    margin: auto;
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

header {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: none;
}

h1 {
    font-size: 18px;
    margin: 0;
}

.status {
    font-size: 12px;
    color: #586174;
}


/* =========================
   メインレイアウト
========================= */

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


/* =========================
   キャンバスエリア
========================= */

.work {
    min-height: 0;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 6px;
    width: 100%;
    flex: 1;
}

.canvas-area {
    grid-column: 2;
    grid-row: 1;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.canvas-wrap {
    position: relative;
    height: min(100%, calc(100vw - 320px));
    max-height: 100%;
    aspect-ratio: 1;
    max-width: 100%;
    width: auto;
}

.canvas-wrap canvas {
    display: block;
    width: 100%;
    height: 100%;
    background: white;
    border: 2px solid #14213d;
    touch-action: none;
    image-rendering: pixelated;
}

.stat {
    font-size: 12px;
    text-align: center;
    min-height: 1.5em;
}


/* =========================
   ツールバー
========================= */

.tools {
    grid-column: 1;
    grid-row: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: flex-start;
    justify-content: center;
}

.palette {
    display: grid;
    grid-template-columns: 1fr;
    gap: 4px;
}

.palette-row {
    display: contents;
}

.palette .swatch {
    grid-column: 1;
}

.palette .pen,
.palette #undo {
    grid-column: 1;
}

.palette .pen {
    grid-column: 1;
    grid-row: 13;
}

.palette #undo {
    grid-column: 1;
    grid-row: 14;
}

.swatch {
    width: var(--button-size);
    height: var(--button-size);
    min-width: var(--button-size);
    min-height: var(--button-size);
    padding: 0;
    border-color: #485267;
    font-weight: 900;
}

.palette-row > .pen,
.palette-row > #undo {
    width: var(--button-size);
    height: var(--button-size);
    min-width: var(--button-size);
    min-height: var(--button-size);
    padding: 2px;
}

.swatch[aria-pressed="true"] {
    outline: 3px solid #e85d04;
    outline-offset: 1px;
}

.eraser {
    font-size: 11px;
}

.pen {
    font-size: 21px;
}

.pen[aria-pressed="true"] {
    background: #ffead9;
    border-color: #e85d04;
}


/* =========================
   右側パネル
========================= */

.panel {
    background: #fff;
    border: 1px solid #d5d9e0;
    border-radius: 12px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 7px;
    min-height: 0;
    overflow: auto;
}

.panel h2 {
    font-size: 14px;
    margin: 0;
}

.panel button {
    width: 100%;
}
.panel-qr button {
    width: 100%;
}
#startCamera{
    background: #aaffab;
    border: 2px solid #0f6d00;
}
#stopCamera{
    background: #ffbaba;
    border: 2px solid #6d0000;
}

.voice-actions {
    display: flex;
    gap: 7px;
    width: 100%;
    grid-column: 1 / -1;
}

.voice-actions > button {
    flex: 1 1 0;
    min-width: 0;
}

.clear-actions {
    display: flex;
    width: 100%;
    gap: 7px;
}

.clear-actions > button {
    flex: 1 1 0;
    min-width: 0;
}
#clear{
    width: 100%;
    background: #d5ffd6;
    border: 2px solid #0f6d00;
    color: #000000;
    font-weight: 900 !important;
}
#clear:active{
    background: #149500;
    color: #fff;
}
#fullscreen{
    position: fixed;
    min-height: 0;
    min-width: 0;
    right: 0;
    top: 0;
    margin-right: 5px;
    margin-top: 5px;
    display: flex;
    text-align: center;
    align-items: center;
    justify-content: center;
    height: 25px;
    font-weight: 900 !important;
    background: #dddddd;
    border: 2px solid #676767;
}

/* =========================
   聞くボタン
========================= */

.listen {
    font-weight: 900 !important;
    min-height: 88px !important;
    font-size: 22px !important;
    border: 3px solid #7c0000 !important;
    background: #ffd5d5;
    transition: background 0.1s, color 0.1s;
}
.voice-actions #describe {
    background: #cdddff;
    font-weight: 900 !important;
    color: #000;
    border: 3px solid #00296f !important;
    transition: background 0.1s, color 0.1s;
}

.listen.on{
    background: #ff0000;
    color: #fff;
}
.voice-actions #describe.on{
    background: #005eff;
    color: #fff;
}

/* =========================
   コマンド入力
========================= */

.command {
    display: flex;
    gap: 5px;
}

.command input {
    min-width: 0;
    width: 100%;
    border: 2px solid #aab2c0;
    border-radius: 8px;
    padding: 7px;
}

.command button {
    width: auto;
}


/* =========================
   チェック項目
========================= */

.checks {
    display: flex;
    gap: 8px;
    color: #166534;
    font-size: 13px;
    font-weight: 700;
}

.checks span {
    display: block;
}


/* =========================
   音声・センサー情報
========================= */

#heard,
#said,
#sensorStatus {
    font-size: 12px;
    line-height: 1.35;
    margin: 0;
    min-height: 1em;
    overflow-wrap: anywhere;
}


/* =========================
   ヘルプ
========================= */

.help {
    font-size: 11px;
    color: #586174;
    margin: 0;
}

.help summary {
    cursor: pointer;
}

.help p {
    margin: 4px 0;
}


/* =========================
   アクセシビリティ
========================= */

.hidden-label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.share-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.share-dialog {
    width: min(560px, calc(100vw - 24px));
    max-width: none;
    padding: 16px;
    border: 2px solid #485267;
    border-radius: 14px;
    color: #14213d;
    text-align: center;
}

.share-dialog::backdrop {
    background: rgb(20 33 61 / 65%);
}

.share-dialog h2 {
    margin: 0 0 8px;
    font-size: 20px;
}

.share-dialog p {
    margin: 8px 0;
}

.share-dialog img {
    display: block;
    width: min(88vw, 480px);
    height: auto;
    margin: 10px auto;
    image-rendering: pixelated;
}

.share-dialog button {
    width: auto;
    min-width: 120px;
}

.readonly-page {
    background: #e7f4e9;
}

.readonly-page .panel {
    background: #f4fbf5;
    border-color: #87b892;
}

.readonly-page .canvas-wrap canvas {
    border-color: #28743a;
}

.readonly-badge {
    padding: 5px 10px;
    border: 1px solid #28743a;
    border-radius: 999px;
    background: #d2edd7;
    color: #145324;
    font-size: 13px;
    font-weight: 700;
}

#cameraPreview {
    width: 100%;
    height: 100%;
    min-width: 1px;
    min-height: 1px;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    border: 0;
    border-radius: 50%;
    background: #172018;
}

.camera-frame {
    position: relative;
    width: min(100%, 78vh, 780px);
    aspect-ratio: 1;
    flex: none;
    border: 3px solid #12341a;
    border-radius: 50%;
    overflow: visible;
    background: #172018;
}

.readonly-page:has(#readerControls) .layout {
    grid-template-columns: minmax(0, 1fr);
}

.readonly-page:has(#readerControls) .work {
    display: none;
}

.readonly-page .work[hidden] {
    display: none;
}

.readonly-page:has(#readerControls) .panel {
    width: min(100%, 900px);
    margin: auto;
    align-items: center;
}

.readonly-page:has(#readerControls) #readerControls {
    width: min(100%, 900px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.readonly-page:has(#readerControls) #cameraPreview {
    width: min(100%, 900px);
    height: 100%;
    max-height: none;
    aspect-ratio: 1 / 1;
}

#resultActions {
    display: flex;
    width: min(100%, 420px);
    flex-direction: column;
    gap: 8px;
}

#resultActions[hidden] {
    display: none;
}

#voiceStatus {
    margin: 0;
    font-size: 12px;
    text-align: center;
}

#receivedArt {
    image-rendering: pixelated;
}

.corner-zone {
    position: fixed;
    z-index: 20;
    bottom: 0;
    width: 70px;
    height: var(--corner-height, 70px);
    min-width: 70px;
    min-height: var(--corner-height, 70px);
    padding: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 3px solid #14213d;
    border-radius: 0px;
    font-size: 25px;
    line-height: 1;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.corner-zone span {
    font-size: 12px;
}

.corner-undo {
    left: 0;
    margin-left: env(safe-area-inset-left);
    background: #dbe7ff;
    border-radius: 16px 16px 0px 0px;
}

.corner-talk {
    right: 0;
    margin-right: env(safe-area-inset-right);
    background: #ffd5d5;
    border-radius: 16px 16px 0px 0px;
}

.corner-talk.on {
    background: #c00000;
    color: #fff;
}


/* =========================
   タブレット・スマホ
========================= */

@media (max-width: 650px), (max-width: 900px) and (orientation: portrait) {

    body {
        height: 100dvh;
        overflow: hidden;
    }

    .app {
        height: 100%;
        min-height: 0;
        padding: 4px 6px;
        gap: 3px;
    }

    header {
        height: 29px;
    }

    h1 {
        font-size: 15px;
    }

    .status {
        font-size: 10px;
    }


    /* =====================
       メインレイアウト
    ===================== */

    .layout {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;
        gap: 5px;
    }


    /* =====================
       キャンバス
    ===================== */

    .work {
        display: flex;
        flex: 0 0 auto;
        flex-direction: column;
        gap: 3px;
    }

    .canvas-area {
        width: 100%;
    }

    /*
       キャンバスを画面横幅の90%にする
       vw = 画面の横幅
    */
    .canvas-wrap {
        height: min(60vw, calc(100dvh - 330px));
        max-width: 90vw;
        max-height: calc(100dvh - 330px);
        aspect-ratio: 1;
    }


    /* =====================
       ツール
    ===================== */

    .tools {
        align-items: center;
    }

    .palette {
        display: flex;
        flex-direction: column;
        gap: 4px;
    }

    .palette-row {
        display: grid;
        grid-template-columns: repeat(7, var(--button-size));
        gap: 3px;
    }

    .palette .swatch {
        grid-column: auto;
    }

    .palette .pen,
    .palette #undo {
        grid-column: auto;
        grid-row: auto;
    }

    /* =====================
       パネル
    ===================== */

    .panel {
        width: calc(100% - 140px);
        margin: 0 auto;
        display: grid;
        grid-template-columns: 1fr 1fr;
        align-content: start;
        overflow: visible;
        padding: 5px;
        gap: 4px;
    }
    .panel-qr {
        overflow: auto;
        padding: 6px;
        gap: 4px;
    }

    .panel h2,
    .panel #heard,
    .panel #said,
    .panel .command,
    .panel #shake,
    .panel #sensorStatus,
    .panel .checks {
        grid-column: 1 / -1;
    }

    .panel .help {
        display: none;
    }

    .panel #describe,
    .panel .clear-actions {
        width: 100%;
        grid-column: 1 / -1;
    }

    .readonly-page:has(#readerControls) .panel {
        width: 100%;
        padding: 4px;
    }

    .readonly-page:has(#readerControls) #cameraPreview {
        width: 100%;
        height: 100%;
        min-height: 0;
        border-radius: 50%;
    }

    .readonly-page:has(#readerControls) .camera-frame {
        width: min(100%, calc(100dvh - 210px));
    }
    .panel #describe {
        background: #cdddff;
        font-weight: 900 !important;
        font-size: 23px !important;
    }

    /* =====================
       聞くボタン
    ===================== */

    .listen {
        min-height: 58px !important;
        font-size: 22px !important;
    }


    /* =====================
       全体のボタン
    ===================== */

    button {
        min-height: 38px;
        padding: 5px 7px;
        font-size: 13px;
    }
}


/* =========================
   小型スマホ
========================= */

@media (max-width: 370px) {

    .layout {
        grid-template-columns: minmax(0, 1fr) 128px;
    }

    .work {
        grid-template-columns: 39px minmax(0, 1fr);
    }

    .panel {
        padding: 5px;
    }

    .help {
        display: none;
    }
}


/* =========================
   横長・低い画面
========================= */

@media (max-height: 580px) and (min-width: 500px) {

    .app {
        padding: 4px 10px;
        gap: 3px;
    }

    header {
        height: 27px;
    }

    .panel {
        padding: 6px;
        gap: 4px;
    }

    .layout {
        grid-template-columns: minmax(0, 1fr) 225px;
    }

}
