:root {
    --primary-bg: #f9f9f9;
    --paper-bg: #fff;
    --grid-line-color: #5d9e60; /* Darkened from #8FCC90 to drastically improve dash visibility */
    --text-color: #333;
    --trace-color1: #a0c4ff;
    --trace-color2: #e1b382;
    --trace-color3: #c2c2c2;
    --grid-size: 70px; 
}

/* iPad向け画像保存ダイアログ */
.save-dialog-overlay {
    position: fixed;
    inset: 0;
    z-index: 100000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(0, 0, 0, 0.62);
    -webkit-overflow-scrolling: touch;
}

.save-dialog {
    width: min(620px, 100%);
    max-height: calc(100vh - 40px);
    max-height: calc(100dvh - 40px);
    overflow: auto;
    padding: 22px;
    border-radius: 18px;
    background: #fff;
    color: #333;
    text-align: center;
    box-shadow: 0 16px 50px rgba(0, 0, 0, 0.3);
}

.save-dialog h2 {
    margin: 0 0 10px;
}

.save-dialog p {
    line-height: 1.6;
}

.save-preview {
    display: block;
    width: auto;
    max-width: 100%;
    max-height: 42vh;
    margin: 16px auto;
    border: 1px solid #ccc;
    background: #fff;
    object-fit: contain;
    -webkit-touch-callout: default;
}

.save-dialog-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
}

.save-dialog-actions button {
    min-height: 48px;
    padding: 10px 18px;
    font-size: 16px;
    touch-action: manipulation;
}

.save-share-btn {
    background: #1976d2;
    color: #fff;
}

.save-status {
    min-height: 1.6em;
    margin-bottom: 0;
    color: #b33;
    font-weight: bold;
}

.app-description {
    margin: 0 0 18px;
    padding: 10px 12px;
    border-radius: 8px;
    background: #eef6ff;
    color: #334;
    font-size: 14px;
    line-height: 1.65;
    text-align: left;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--primary-bg);
    font-family: 'UD デジタル 教科書体 NK-B', 'UD Digi Kyokasho NK-B', 'UDDigiKyokashoN-B', 'Klee One', 'Yu Gothic', sans-serif;
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 20px;
}

/* Control Panel */
#control-panel {
    background: white;
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    width: 320px;
    margin-right: 30px;
    flex-shrink: 0;
    position: sticky;
    top: 20px;
}

.app-title {
    font-size: 22px;
    text-align: center;
    color: #fff;
    background: linear-gradient(135deg, #FF9800, #FF5722);
    padding: 12px 10px;
    border-radius: 10px;
    margin-bottom: 15px;
    line-height: 1.4;
    letter-spacing: 2px;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
}

#control-panel h2 {
    margin-bottom: 20px;
    font-size: 18px;
    color: #4CAF50;
}

#control-panel h3 {
    font-size: 16px;
    margin-bottom: 10px;
    color: #555;
}

.control-group {
    margin-bottom: 15px;
}

.control-group label {
    display: block;
    margin-bottom: 5px;
    font-size: 14px;
    font-weight: bold;
}

.control-group input[type="text"],
.control-group input[type="number"],
.control-group select {
    width: 100%;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
}

hr {
    border: none;
    border-top: 1px solid #eee;
    margin: 20px 0;
}

button {
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 10px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    width: 100%;
    margin-top: 5px;
    transition: background 0.2s;
}

button:hover {
    background-color: #45a049;
}

.camera-btn {
    background-color: #2196F3;
}
.camera-btn:hover { background-color: #1976D2; }

#print-btn {
    margin-top: 30px;
    font-size: 16px;
    font-weight: bold;
    padding: 15px;
    background-color: #FF5722;
}
#print-btn:hover { background-color: #E64A19; }

#pdf-btn {
    margin-top: 10px;
    font-size: 16px;
    font-weight: bold;
    padding: 15px;
    background-color: #388E3C;
}
#pdf-btn:hover { background-color: #2E7D32; }

/* A4 Page Setup */
.page.a4 {
    width: 210mm;
    height: 297mm;
    background: var(--paper-bg);
    box-shadow: 0 0 15px rgba(0,0,0,0.15);
    overflow: hidden;
    position: relative;
    flex-shrink: 0;
    transform-origin: top left;
    transform: scale(var(--worksheet-scale, 1));
    margin-bottom: calc(-297mm * (1 - var(--worksheet-scale, 1)));
    margin-right: calc(-210mm * (1 - var(--worksheet-scale, 1)));
}

.page-layout {
    display: flex;
    width: 100%;
    height: 100%;
    padding: 10mm 15mm 10mm 15mm;
}

/* Left Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Header/Top Area */
.top-section {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    margin-bottom: 15px;
}

.big-char-box {
    width: 140px;
    height: 140px;
    border: 4px solid var(--grid-line-color);
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 30px;
    flex-shrink: 0;
}

/* Background Cross Dashed Lines - 実DOM要素を使用（html2canvas互換） */
.big-char-grid-bg,
.practice-grid-box-bg {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
}
/* Horizontal dashed line */
.grid-line-h {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 0;
    border-top: 1.5px dashed var(--grid-line-color);
    margin-top: -0.75px;
    pointer-events: none;
}
/* Vertical dashed line */
.grid-line-v {
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 0;
    border-left: 1.5px dashed var(--grid-line-color);
    margin-left: -0.75px;
    pointer-events: none;
}

/* =============================================
   Grid Style Toggles
   ============================================= */
.grid-style-box .grid-line-h,
.grid-style-box .grid-line-v {
    display: none !important;
}

.grid-style-none .grid-line-h,
.grid-style-none .grid-line-v,
.grid-style-none .practice-grid-box-bg {
    display: none !important;
}

.grid-style-none .big-char-box,
.grid-style-none .practice-col,
.grid-style-none .practice-grid-box,
.grid-style-none .word-practice-row,
.grid-style-none .word-practice-row .practice-grid-box {
    border: none !important;
}

.big-char-box span#big-char {
    font-size: 115px;
    color: #FFC107; /* The large one in the example is yellow with an outline, let's keep it simple or orange */
    z-index: 1;
    line-height: 1;
}

.char-group {
    display: flex;
    flex-direction: column;
    font-size: 32px;
    color: #F57C00;
    line-height: 1.2;
    margin-top: 5px;
    font-weight: 600;
}

.char-group span {
    text-align: center;
}

.char-group span.highlighted {
    display: inline-block;
    width: 44px;
    height: 44px;
    line-height: 44px;
    border-radius: 50%;
    background-color: #FFCCBC;
    border: 2px solid #333;
    color: #D84315;
}

/* Practice Grids Wrapper */
.practice-grids-wrapper {
    flex: 1;
    display: flex;
    justify-content: flex-start;
    margin-bottom: 15px;
}

/* Each Column Wrapper */
.practice-grids-container {
    display: flex;
    gap: 15px; /* space between columns */
    padding: 5px;
    flex-direction: row-reverse; /* Japanese vertical writing goes Right to Left */
    align-items: flex-start; /* Stop columns from stretching vertically */
}

/* A single practice column (vertical) */
.practice-col {
    display: flex;
    flex-direction: column;
    border: 2px solid var(--grid-line-color);
    height: max-content;
}

.practice-grid-box {
    width: var(--grid-size);
    height: var(--grid-size);
    border-bottom: 2px solid var(--grid-line-color);
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
}
.practice-grid-box:last-child {
    border-bottom: none;
}

.practice-char {
    font-size: 50px;
    z-index: 1;
    font-weight: 600;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    line-height: 1;
    text-align: center;
}
.trace-sample { color: var(--text-color); } /* Black for sample */
.trace-1 { color: #c0c0c0; } /* light grey */
.trace-2 { color: #c0c0c0; } /* light grey */
.trace-3 { color: #c0c0c0; } /* light grey */
.trace-empty { display: none; }

/* Vertical text: rotate prolonged sound mark (ー) for vertical writing */
.practice-char.vertical-text.is-chouon {
    transform: translate(-50%, -50%) rotate(90deg);
}

/* Bottom Section */
.bottom-section {
    min-height: 200px;
    display: flex;
    flex-direction: column;
    margin-bottom: 10px;
}

.instruction {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 10px;
}

.words-container {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 10px;
    align-items: flex-start;
    justify-content: space-around;
    flex: 1;
}

.word-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.word-item img {
    max-width: 100px;
    max-height: 60px;
    object-fit: contain;
    border: 2px solid #ccc;
    border-radius: 8px;
    padding: 5px;
    background: white;
}

.word-practice-area {
    display: flex;
    flex-direction: row-reverse;
    gap: 5px;
    align-items: flex-start;
}
.word-practice-row {
    display: flex;
    flex-direction: row;
    border: 2px solid var(--grid-line-color);
    width: max-content;
}
.word-practice-row .practice-grid-box {
    border-bottom: none;
    border-right: 2px solid var(--grid-line-color);
}

.sidebar {
    width: 35mm;
    display: flex;
    justify-content: space-between;
    padding-left: 10px;
}

.title-vertical {
    writing-mode: vertical-rl;
    text-orientation: upright;
    font-size: 24px;
    font-weight: bold;
    letter-spacing: 5px;
    border: 2px solid #5C94B3;
    padding: 15px 5px;
    color: #333;
    height: 100%;
}

.name-box {
    width: 80px;
    height: 70%;
    border: 2px solid #555;
    align-self: flex-end;
    display: flex;
    flex-direction: column;
}

.name-label {
    font-size: 14px;
    padding: 5px;
    text-align: center;
    writing-mode: vertical-rl;
    text-orientation: upright;
}

/* Modal */
#camera-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}
.camera-modal {
    background: white;
    padding: 20px;
    border-radius: 12px;
    text-align: center;
}
video#live-video {
    width: 100%;
    max-width: 400px;
    border-radius: 8px;
    margin: 15px 0;
    transform: scaleX(-1);
}
.camera-controls {
    display: flex;
    gap: 10px;
    justify-content: center;
}
#cancel-camera-btn {
    background-color: #9e9e9e;
}

/* Print Styles */
@media print {
    @page {
        size: A4 portrait;
        margin: 0;
    }
    .no-print { display: none !important; }
    body {
        background: transparent;
        margin: 0;
        padding: 0;
        width: 210mm;
        height: 297mm;
    }
    .page.a4 {
        margin: 0;
        border: none;
        box-shadow: none;
        width: 210mm;
        height: 297mm;
        overflow: hidden;
        page-break-inside: avoid;
        page-break-after: avoid;
    }
}

/* =============================================
   Large Grid Mode
   ============================================= */
.large-grid {
    --grid-size: 140px;
}

.large-grid .big-char-box {
    width: 180px;
    height: 180px;
}

.large-grid .big-char-box span#big-char {
    font-size: 150px;
}

.large-grid .char-group {
    font-size: 40px;
}

.large-grid .char-group span.highlighted {
    width: 52px;
    height: 52px;
    line-height: 52px;
}

.large-grid .practice-char {
    font-size: 100px;
}

.large-grid .bottom-section {
    --grid-size: 90px;
}

.large-grid .bottom-section .practice-char {
    font-size: 65px;
}

.large-grid .word-item img {
    max-width: 140px;
    max-height: 90px;
}

.large-grid .words-container {
    justify-content: center;
}

.large-grid .instruction {
    font-size: 20px;
}

/* =============================================
   Mobile Toggle Button (hidden on desktop)
   ============================================= */
#mobile-settings-toggle {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 999;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #FF9800, #FF5722);
    color: white;
    font-size: 28px;
    border: none;
    box-shadow: 0 4px 15px rgba(255, 87, 34, 0.4);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

#mobile-settings-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(255, 87, 34, 0.5);
}

/* =============================================
   Mobile Responsive Styles
   ============================================= */
@media screen and (max-width: 820px) {
    body {
        flex-direction: column;
        align-items: flex-start;
        padding: 10px;
    }

    #mobile-settings-toggle {
        display: flex;
    }

    #control-panel {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        margin: 0;
        border-radius: 0;
        z-index: 998;
        overflow-y: auto;
        display: none;
        padding: 20px 20px 100px 20px;
        box-shadow: none;
    }

    #control-panel.mobile-open {
        display: block;
    }

    #mobile-close-btn {
        display: block;
        position: sticky;
        top: 0;
        background: #f44336;
        color: white;
        border: none;
        padding: 12px;
        font-size: 16px;
        font-weight: bold;
        width: 100%;
        border-radius: 8px;
        margin-bottom: 15px;
        cursor: pointer;
        z-index: 10;
    }

    .page.a4 {
        box-shadow: 0 0 8px rgba(0,0,0,0.1);
    }

    .practice-grids-container {
        overflow-x: auto;
    }

    .bottom-section {
        min-height: auto;
    }
}
