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

/* 游教科書体のフォント定義 */
@font-face {
    font-family: 'Yu Kyokasho';
    src: local('Yu Kyokasho'), 
         local('YuKyokasho'), 
         local('游教科書体'), 
         local('Yu Kyokasho Medium'),
         local('YuKyokasho-Medium'),
         local('游教科書体 Medium');
    font-weight: normal;
    font-style: normal;
}

body {
    font-family: 'MS Gothic', sans-serif;
    background-color: #f5f5f5;
}

.container {
    display: flex;
    height: 100vh;
}

/* コントロールパネル */
.controls {
    width: 350px;
    background: white;
    padding: 20px;
    border-right: 1px solid #ddd;
    overflow-y: auto;
    flex-shrink: 0;
}

.controls h2 {
    color: #333;
    margin-bottom: 20px;
    border-bottom: 2px solid #007bff;
    padding-bottom: 10px;
}

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

.control-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    color: #555;
}

.control-group small {
    color: #666;
    font-size: 12px;
    display: block;
    margin-top: 4px;
}

.control-group input,
.control-group select,
.control-group textarea {
    width: 100%;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
}

.control-group textarea {
    resize: vertical;
    font-family: monospace;
}

.control-group button {
    width: 100%;
    padding: 12px;
    background: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    margin-bottom: 5px;
}

.control-group button:hover {
    background: #0056b3;
}

.control-group button:last-child {
    margin-bottom: 0;
}

/* 特別なボタンスタイル */
#bulk-print-btn {
    background: #28a745;
}

#bulk-print-btn:hover {
    background: #1e7e34;
}

#save-settings {
    background: #17a2b8;
}

#save-settings:hover {
    background: #138496;
}

#load-settings-btn {
    background: #6c757d;
}

#load-settings-btn:hover {
    background: #545b62;
}

/* プレビューエリア */
.preview {
    flex: 1;
    display: block;
    padding: 5px;
    overflow: auto;
    min-width: 0;
    min-height: 0;
}

.certificate {
    position: relative;
    background: white;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    border: 1px solid #ddd;
    transform: scale(0.45);
    transform-origin: top left;
    margin: 0;
    /* A4縦サイズ - 実寸 */
    width: 210mm !important;
    height: 297mm !important;
}

/* A4横サイズ - 実寸 */
.certificate.A4-landscape {
    width: 297mm !important;
    height: 210mm !important;
}

/* A3横サイズ - 実寸 */
.certificate.A3-landscape {
    width: 420mm !important;
    height: 297mm !important;
}

#bg-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 1;
}

/* テキスト要素 */
.text-element {
    position: absolute;
    z-index: 10;
    cursor: pointer;
    user-select: none;
}

.text-element.active {
    outline: 2px dashed #007bff;
    background: rgba(0, 123, 255, 0.1);
}

/* テキスト共通スタイル */
.vertical-text {
    font-family: 'Yu Kyokasho', 'YuKyokasho', '游教科書体', 'Yu Mincho', 'YuMincho', '游明朝', 'MS Mincho', 'ヒラギノ明朝 ProN', serif !important;
    color: #333;
    line-height: 1.8;
    padding: 10px;
    white-space: pre-wrap;
    word-break: keep-all;
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid transparent;
    border-radius: 4px;
}

.vertical-text:hover {
    background: rgba(255, 255, 255, 0.9);
    border-color: #007bff;
    box-shadow: 0 2px 8px rgba(0, 123, 255, 0.2);
}

.vertical-text:focus {
    outline: none;
    background: white;
    border-color: #007bff;
    box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}

.vertical-text::placeholder {
    color: #95a5a6;
    opacity: 0.8;
}

/* 各要素のデフォルトフォントサイズ（実サイズ） */
/* A4横・A4縦用 */
#title .vertical-text {
    font-size: 30px !important; /* 実サイズ */
}

#school .vertical-text {
    font-size: 28px !important; /* 実サイズ */
}

#name .vertical-text {
    font-size: 32px !important; /* 実サイズ */
}

/* A3横用のフォントサイズ（実サイズ） */
.certificate.A3-landscape #title .vertical-text {
    font-size: 45px !important; /* 実サイズ */
}

.certificate.A3-landscape #school .vertical-text {
    font-size: 36px !important; /* 実サイズ */
}

.certificate.A3-landscape #name .vertical-text {
    font-size: 45px !important; /* 実サイズ */
}

/* 縦書きテキスト（A4横・A3横用） */
.certificate.A4-landscape .vertical-text,
.certificate.A3-landscape .vertical-text {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    min-height: 60px;
    min-width: 40px;
}

/* 下寄せ氏名欄（A3用） */
.certificate.A3-landscape .text-element.bottom-aligned {
    height: 450px; /* A3は10文字分の高さを確保 */
}

.text-element.bottom-aligned .vertical-text {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    white-space: nowrap;
    line-height: 1.8;
    min-width: 80px;
    padding: 10px;
}

/* A4横用の下寄せ氏名欄（高さを小さく） */
.certificate.A4-landscape .text-element.bottom-aligned {
    height: 300px; /* A4横は7文字分程度の高さ */
}

/* A4縦用の下寄せ氏名欄（横書きなので幅を制限） */
.certificate:not(.A4-landscape):not(.A3-landscape) .text-element.bottom-aligned {
    width: 300px; /* A4縦は横書きなので幅を制限 */
    height: auto;
}

/* A4縦用の下寄せ氏名欄（横書き） */
.certificate:not(.A4-landscape):not(.A3-landscape) .text-element.bottom-aligned .vertical-text {
    writing-mode: horizontal-tb;
    text-orientation: mixed;
    text-align: right;
    align-items: flex-end;
}

/* 横書きテキスト（A4縦用） */
.certificate:not(.A4-landscape):not(.A3-landscape) .vertical-text {
    writing-mode: horizontal-tb;
    text-orientation: mixed;
    min-height: 40px;
    min-width: 60px;
}

/* 初期位置設定 */
#title {
    top: 50px;
    right: 50px;
}

#school {
    top: 100px;
    right: 100px;
}

#name {
    top: 150px;
    right: 150px;
}

/* 印刷時のスタイル */
@media print {
    body {
        margin: 0;
        padding: 0;
        background: white;
    }
    
    .controls {
        display: none;
    }
    
    .container {
        display: block;
        height: auto;
    }
    
    .preview {
        padding: 0;
        margin: 0;
        display: block;
    }
    
    .certificate {
        box-shadow: none;
        border: none;
        position: absolute;
        top: 0;
        left: 0;
        transform: scale(1) !important;
        transform-origin: top left !important;
    }
    
    .certificate.A4-PORTRAIT-print {
        width: 210mm !important;
        height: 297mm !important;
    }
    
    .certificate.A4-LANDSCAPE-print {
        width: 297mm !important;
        height: 210mm !important;
    }
    
    .certificate.A3-LANDSCAPE-print {
        width: 420mm !important;
        height: 297mm !important;
    }
    
    /* 印刷時のテキスト要素スタイル */
    .text-element {
        position: absolute !important;
    }
    
    .vertical-text {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
    }
    
    .text-element.active {
        outline: none !important;
        background: transparent !important;
    }
}

/* レスポンシブ対応 */
@media (max-width: 768px) {
    .container {
        flex-direction: column;
    }
    
    .controls {
        width: 100%;
        max-height: 300px;
        order: 2;
    }
    
    .preview {
        order: 1;
        padding: 10px;
    }
    
    .certificate {
        transform: scale(0.3);
        transform-origin: center;
    }
}