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

html, body {
    height: 100%;
    width: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', sans-serif;
    background-color: #f5f5f5;
    color: #333;
    font-size: 16px;
    line-height: 1.6;
}

.container {
    max-width: 100%;
    padding: 0;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px 16px;
    text-align: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

header h1 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 4px;
}

.subtitle {
    font-size: 12px;
    opacity: 0.9;
}

/* Main Content */
.input-section,
.display-section {
    padding: 16px;
    flex: 1;
    overflow-y: auto;
}

.input-section h2,
.display-section h2 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
}

/* TextArea Input */
.text-input {
    width: 100%;
    min-height: 300px;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 16px;
    font-family: inherit;
    resize: vertical;
    margin-bottom: 16px;
    -webkit-appearance: none;
    appearance: none;
}

.text-input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* Mode Selection */
.mode-selection {
    background-color: white;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 16px;
}

.mode-selection h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
}

/* 選択肢は横並びのボタンにして、説明は選んでいるものだけ1行で出す */
.choice-row + .choice-row {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid #f0f0f0;
}

.segmented {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 4px;
    padding: 4px;
    background-color: #f0f1f5;
    border-radius: 10px;
}

.segmented label {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 6px 8px;
    border-radius: 7px;
    font-size: 14px;
    font-weight: 600;
    color: #666;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.segmented input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    pointer-events: none;
}

.segmented label:has(input:checked) {
    background-color: white;
    color: #4a5bd0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.segmented label:has(input:focus-visible) {
    outline: 2px solid #667eea;
    outline-offset: 1px;
}

.choice-note {
    margin-top: 8px;
    font-size: 12px;
    color: #888;
    line-height: 1.5;
}

.choice-note:empty {
    display: none;
}

/* ランダムの黒塗り率: ラベル・スライダー・値を1行に */
.percentage-control {
    margin-top: 10px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.percentage-control.hidden {
    display: none;
}

.percentage-control label {
    font-size: 13px;
    font-weight: 600;
    color: #555;
    white-space: nowrap;
}

#randomPercentage {
    flex: 1;
    min-width: 0;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: linear-gradient(to right, #667eea, #764ba2);
    border-radius: 3px;
    outline: none;
}

#randomPercentage::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: white;
    border: 2px solid #667eea;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

#randomPercentage::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: white;
    border: 2px solid #667eea;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

#percentageValue {
    font-size: 14px;
    font-weight: 700;
    color: #4a5bd0;
    min-width: 40px;
    text-align: right;
}

/* Buttons */
.button-group {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.btn {
    padding: 12px 16px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    -webkit-appearance: none;
    appearance: none;
    -webkit-user-select: none;
    user-select: none;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.btn-primary {
    background-color: #667eea;
    color: white;
    flex: 1;
    min-width: 120px;
}

.btn-primary:active {
    background-color: #5568d3;
    transform: scale(0.98);
}

.btn-secondary {
    background-color: #e0e0e0;
    color: #333;
    flex: 1;
    min-width: 120px;
}

.btn-secondary:active {
    background-color: #d0d0d0;
    transform: scale(0.98);
}

.btn-small {
    padding: 8px 12px;
    font-size: 12px;
    min-width: 100px;
}

/* Display Section */
.display-section {
    display: none;
}

.display-section.active {
    display: flex;
    flex-direction: column;
}

.display-section.hidden {
    display: none !important;
}

/* 学習画面の操作: よく使う3つを1行に収め、本文をすぐ下に置く */
.study-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.tool-btn {
    flex: none;
    padding: 8px 14px;
    border: 1px solid #d5d5d5;
    border-radius: 999px;
    background-color: white;
    color: #333;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    line-height: 1.4;
    white-space: nowrap;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

.tool-btn:hover {
    background-color: #f0f0f0;
}

.tool-btn:active {
    background-color: #e4e4e4;
}

/* 「戻る」は左端に離して置き、目立たせない */
.tool-back {
    margin-right: auto;
    border-color: transparent;
    background-color: transparent;
    color: #666;
    padding-left: 4px;
}

/* いちばん使う操作 */
.tool-main {
    border-color: #667eea;
    background-color: #667eea;
    color: white;
}

.tool-main:hover {
    background-color: #5568d3;
}

.tool-main:active {
    background-color: #4a5bc0;
}

/* 本文の下に置く、学習のあとで使う操作 */
.more-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

/* 学習中はヘッダーを細くして、本文に場所を譲る */
body.studying header {
    padding: 8px 16px;
}

body.studying header h1 {
    font-size: 15px;
    margin-bottom: 0;
}

body.studying .subtitle {
    display: none;
}

/* Text Display Area */
.text-display {
    background-color: white;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 16px;
    word-break: break-word;
    user-select: none;
    -webkit-user-select: none;
    min-height: 200px;
    border: 1px solid #e0e0e0;
    font-size: 16px;
    /* Ensure consistent line-height for all content */
    line-height: 1.8;
}

/* Blurred word/character - Fixed dimensions to prevent layout shift */
.blurred {
    background-color: #000;
    color: transparent;
    display: inline-block;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    transition: background-color 0.15s ease, color 0.15s ease;
    position: relative;
    vertical-align: text-bottom;
    /* Keep padding and margin consistent for all states */
    padding: 0 2px;
    margin: 0 1px;
    /* Ensure text doesn't overflow and maintains space */
    font-weight: inherit;
    font-size: inherit;
}

.blurred.revealed {
    background-color: transparent;
    color: #333;
    /* Keep the same padding and margin when revealed */
    padding: 0 2px;
    margin: 0 1px;
}

/* 句・文の単位: 行をまたいでも1本の帯に見えるようにする */
.blurred.chunk {
    display: inline;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
    margin: 0;
}

/* 保存したパターン */
.pattern-bar {
    background-color: white;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 12px 16px;
    margin-bottom: 16px;
}


.pattern-save-box summary {
    font-size: 14px;
    font-weight: 600;
    color: #4a5bd0;
    cursor: pointer;
    padding: 2px 0;
}

.pattern-save-box[open] summary {
    margin-bottom: 10px;
}

.pattern-save {
    display: flex;
    gap: 8px;
    align-items: center;
}

.pattern-name {
    flex: 1;
    min-width: 0;
    padding: 7px 12px;
    border: 1px solid #d5d5d5;
    border-radius: 999px;
    font: inherit;
}

.pattern-message {
    margin-bottom: 8px;
    font-size: 13px;
    color: #4a5bd0;
}

.pattern-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}

.pattern-list:empty {
    display: none;
}

.pattern-chip {
    display: inline-flex;
    align-items: stretch;
    border: 1px solid #667eea;
    border-radius: 999px;
    overflow: hidden;
}

.pattern-apply,
.pattern-delete,
.library-open {
    border: none;
    background: none;
    font: inherit;
    cursor: pointer;
    color: inherit;
}

.pattern-apply {
    padding: 6px 4px 6px 14px;
    font-size: 13px;
    color: #4a5bd0;
    font-weight: 600;
}

.pattern-delete {
    padding: 6px 12px 6px 8px;
    font-size: 14px;
    color: #888;
}

.pattern-delete:hover {
    color: #c0392b;
}

.pattern-hint {
    margin-top: 10px;
    font-size: 12px;
    color: #777;
    line-height: 1.6;
}

.library-box {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid #e0e0e0;
}

.library-box h3 {
    font-size: 14px;
    color: #555;
    margin-bottom: 8px;
}

.library-list {
    list-style: none;
}

.library-list li {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f0f0f0;
}

.library-open {
    flex: 1;
    min-width: 0;
    padding: 10px 4px;
    text-align: left;
    font-size: 14px;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.library-open:hover {
    color: #4a5bd0;
}

/* 本文のすぐ下の小さな案内 */
.tap-hint {
    margin: -8px 0 14px;
    font-size: 12px;
    color: #999;
}

/* 読み上げ（英文のときだけ表示）: ボタンと同じ並びに置く */
.tts-control {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 14px 7px 10px;
    border: 1px solid #d5d5d5;
    border-radius: 999px;
    background-color: white;
    font-size: 13px;
    font-weight: 600;
    color: #333;
    cursor: pointer;
    white-space: nowrap;
}

.tts-control.hidden {
    display: none !important;
}

.tts-control:has(input:checked) {
    border-color: #667eea;
    color: #4a5bd0;
    background-color: #f2f4ff;
}

.tts-control input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: #667eea;
    flex-shrink: 0;
}

/* Responsive Design */
/* Mobile First (iPhone) */
@media (max-width: 600px) {
    header {
        padding: 20px 16px;
    }
    
    header h1 {
        font-size: 20px;
    }
    
    .subtitle {
        font-size: 11px;
    }
    
    .input-section,
    .display-section {
        padding: 16px;
    }
    
    .text-input {
        min-height: 220px;
    }
    
    .mode-selection {
        padding: 12px;
        margin-bottom: 16px;
    }
    
    .mode-selection h3 {
        font-size: 13px;
    }
    
    .text-display {
        padding: 16px;
        min-height: 150px;
        font-size: 15px;
        line-height: 1.8;
    }
    
    /* 「開始する」を主役に、リセットは横に小さく */
    .btn-primary {
        flex: 3;
        min-width: 0;
    }

    .btn-secondary {
        flex: 1;
        min-width: 0;
    }
}

/* Tablet */
@media (min-width: 600px) and (max-width: 1200px) {
    .container {
        max-width: 90%;
        margin: 0 auto;
    }
    
    header {
        padding: 24px 20px;
    }
    
    header h1 {
        font-size: 26px;
    }
    
    .input-section,
    .display-section {
        padding: 20px;
    }
    
    .text-input {
        min-height: 280px;
        font-size: 15px;
    }
    
    .mode-selection {
        padding: 14px;
        margin-bottom: 16px;
    }
    
    .text-display {
        padding: 18px;
        min-height: 250px;
        font-size: 15px;
        line-height: 1.8;
    }
    
    .button-group {
        flex-direction: row;
    }
    
    .btn-primary {
        flex: 2;
    }
    
    .btn-secondary {
        flex: 1;
    }
}

/* Desktop */
@media (min-width: 1200px) {
    .container {
        max-width: 900px;
        margin: 0 auto;
        box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
        border-radius: 8px 8px 0 0;
    }
    
    header {
        padding: 30px 40px;
        border-radius: 8px 8px 0 0;
    }
    
    header h1 {
        font-size: 32px;
        margin-bottom: 8px;
    }
    
    .subtitle {
        font-size: 14px;
    }
    
    .input-section,
    .display-section {
        padding: 30px 40px;
    }
    
    .input-section h2,
    .display-section h2 {
        font-size: 18px;
        margin-bottom: 16px;
    }
    
    .text-input {
        min-height: 350px;
        font-size: 16px;
        padding: 14px;
    }
    
    .mode-selection {
        padding: 16px;
        margin-bottom: 16px;
    }
    
    .mode-selection h3 {
        font-size: 15px;
    }
    
    .text-display {
        padding: 24px;
        min-height: 350px;
        font-size: 17px;
        line-height: 1.8;
    }
    
    .button-group {
        flex-direction: row;
        gap: 12px;
    }
    
    .btn-primary {
        flex: 2;
        min-width: 150px;
    }
    
    .btn-secondary {
        flex: 1;
        min-width: 120px;
    }
    
    .btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
    }
}

/* iPhone specific optimizations */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
        padding-top: max(0px, env(safe-area-inset-top));
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }
}

/* Prevent zoom on input focus for iOS */
input,
textarea,
select {
    font-size: 16px !important;
}

/* Link to the articles section */
/* Guide (static explanation below the tool) */
.guide-section {
    max-width: 640px;
    margin: 0 auto;
    padding: 32px 16px 0;
    color: #444;
    font-size: 14px;
    line-height: 1.8;
}

.guide-section h2 {
    font-size: 16px;
    color: #333;
    margin: 24px 0 8px;
}

.guide-section h2:first-child {
    margin-top: 0;
}

.guide-steps,
.guide-list {
    padding-left: 1.4em;
}

.guide-steps li,
.guide-list li {
    margin-bottom: 8px;
}

.guide-section a {
    color: #5568d3;
}

.guide-more {
    margin-top: 16px;
}

/* Touch feedback */
.blurred:active {
    opacity: 0.8;
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* キーボード操作: いま選んでいる黒塗りが分かるようにする */
.blurred:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

/* 何も入力せずに「開始する」を押したとき */
.text-input.needs-text {
    border-color: #e05a4f;
}

/* 2回押しで確定する削除ボタン（1回目を押したあと） */
[data-armed] {
    color: #c0392b !important;
    font-weight: 700;
}

.guide-text {
    margin: 0 0 14px;
}
