/* 
 * Enhanced Japanese font configuration
 * Optimized for performance, readability and cross-platform consistency
 */

/* === FONT DECLARATIONS === */

/* Noto Sans JP - Variable Font (Weight axis) - Primary Japanese font */
@font-face {
    font-family: 'Noto Sans JP';
    src: local('Noto Sans JP Variable'),
         url('../fonts/NotoSansJP-Variable.woff2') format('woff2-variations'),
         url('https://fonts.gstatic.com/s/notosansjp/v52/-F6jfjtqLzI2JPCgQBnw7HFyzSD-AsregP8VFBEj75-xRnz2MKQ.woff2') format('woff2-variations');
    font-weight: 100 900; /* Variable font weight range */
    font-style: normal;
    font-display: swap;
    unicode-range: U+3000-30FF, U+FF00-FFEF, U+4E00-9FAF; /* Japanese only */
    font-feature-settings: "palt"; /* Proportional alternate width */
}

/* Noto Sans - Variable Font for Latin characters */
@font-face {
    font-family: 'Noto Sans';
    src: local('Noto Sans Variable'),
         url('https://fonts.gstatic.com/s/notosans/v35/o-0mIpQlx3QUlC5A4PNB6Ryc.woff2') format('woff2-variations');
    font-weight: 100 900; /* Variable font weight range */
    font-style: normal;
    font-display: swap;
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; /* Latin only */
}

/* BIZ UDPGothic - Japanese UI font - Better for interfaces */
@font-face {
    font-family: 'BIZ UDPGothic';
    src: local('BIZ UDPGothic'),
         url('https://fonts.gstatic.com/s/bizudpgothic/v14/hES36Xn7FA8Cn5rd6sD9EXYZbUOxEQkVjxg.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
    unicode-range: U+3000-30FF, U+FF00-FFEF, U+4E00-9FAF; /* Japanese only */
}

/* BIZ UDPGothic Bold - Japanese UI font bold */
@font-face {
    font-family: 'BIZ UDPGothic';
    src: local('BIZ UDPGothic Bold'),
         url('https://fonts.gstatic.com/s/bizudpgothic/v14/hES56Xn7FA8Cn5rd6sD9EXYZbUOxEKieHVkSYw.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
    unicode-range: U+3000-30FF, U+FF00-FFEF, U+4E00-9FAF; /* Japanese only */
}

/* Zen Kaku Gothic New - Modern Japanese with excellent readability */
@font-face {
    font-family: 'Zen Kaku Gothic New';
    src: local('Zen Kaku Gothic New'),
         url('https://fonts.gstatic.com/s/zenkakugothicnew/v18/gNMVW2drQpDw0htJqGeUBGOZ5oiso_qWNGKKhBMJ4JM.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
    unicode-range: U+3000-30FF, U+FF00-FFEF, U+4E00-9FAF; /* Japanese only */
}

/* Zen Kaku Gothic New Bold - Modern Japanese Bold */
@font-face {
    font-family: 'Zen Kaku Gothic New';
    src: local('Zen Kaku Gothic New Bold'),
         url('https://fonts.gstatic.com/s/zenkakugothicnew/v18/gNMYW2drQpDw0htJqGeUBGOZ5oiso_qWNHeTeOqQiJHwlA.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
    unicode-range: U+3000-30FF, U+FF00-FFEF, U+4E00-9FAF; /* Japanese only */
}

/* === GLOBAL JAPANESE TYPOGRAPHY SETTINGS === */

/* Base font family settings for the entire site */
:root {
    /* Primary font stack for Japanese content */
    --font-japanese: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "BIZ UDPGothic", "Noto Sans JP", "Zen Kaku Gothic New", "Yu Gothic", "YuGothic", -apple-system, BlinkMacSystemFont, sans-serif;
    
    /* Secondary font stack for headings */
    --font-japanese-headings: "Hiragino Sans", "Noto Sans JP", "BIZ UDPGothic", "Zen Kaku Gothic New", "Yu Gothic", "YuGothic", -apple-system, BlinkMacSystemFont, sans-serif;
    
    /* Typography settings */
    --jp-line-height: 2.1;
    --jp-letter-spacing: 0.02em;
    --jp-heading-letter-spacing: 0.03em;
    
    /* Font weights using the variable font capabilities */
    --jp-font-weight-light: 300;
    --jp-font-weight-regular: 400;
    --jp-font-weight-medium: 500;
    --jp-font-weight-bold: 700;
    
    /* Font size variables */
    --jp-font-size-base: 1rem;
    --jp-font-size-h1: 2.5rem;
    --jp-font-size-h2: 2rem;
    --jp-font-size-h3: 1.7rem;
    --jp-font-size-h4: 1.4rem;
    --jp-font-size-h5: 1.2rem;
    --jp-font-size-h6: 1.1rem;
    --jp-font-size-p: var(--jp-font-size-base);
    --jp-font-size-div: var(--jp-font-size-base);
    
    /* Font smoothing */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* Global font application with enhanced text rendering */
body {
    font-family: var(--font-japanese);
    font-feature-settings: "palt"; /* Proportional alternate width for Japanese text */
    font-variation-settings: "wght" var(--jp-font-weight-regular);
    text-size-adjust: 100%;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: normal;
    line-break: strict;
}

/* Target all headings for Japanese optimization */
h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-japanese-headings);
    font-feature-settings: "palt";
    font-variation-settings: "wght" var(--jp-font-weight-bold);
    letter-spacing: var(--jp-heading-letter-spacing);
    line-height: 1.4;
    word-break: keep-all;
    margin-top: 1.5em;
    margin-bottom: 0.8em;
}

/* Specific heading styles for better Japanese typography */
h1 {
    font-feature-settings: "palt" 1;
    letter-spacing: 0.04em;
    font-size: var(--jp-font-size-h1);
}

h2 {
    font-feature-settings: "palt" 1;
    letter-spacing: 0.03em;
    font-size: var(--jp-font-size-h2);
}

h3 {
    font-size: var(--jp-font-size-h3);
}

h4 {
    font-size: var(--jp-font-size-h4);
}

h5 {
    font-size: var(--jp-font-size-h5);
}

h6 {
    font-size: var(--jp-font-size-h6);
}

/* Target all paragraphs for Japanese optimization */
p, li, dl, blockquote, figcaption, .post-full-content, .gh-content {
    font-family: var(--font-japanese) !important;
    line-height: var(--jp-line-height);
    letter-spacing: var(--jp-letter-spacing);
    font-feature-settings: "palt";
    font-size: var(--jp-font-size-p) !important;
    margin-bottom: 1.5em;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: normal;
    line-break: strict;
}

/* Standard div elements */
div {
    font-size: var(--jp-font-size-div);
    line-height: var(--jp-line-height);
}

/* Target all navigation and UI elements */
.site-title, .site-description, .post-title, .post-excerpt, .author-name,
.gh-head, .gh-foot, .gh-nav, .gh-header, .nav, .menu, .button, .tag, 
.card, .gh-card, input, textarea, select, button {
    font-family: var(--font-japanese) !important;
    font-feature-settings: "palt" !important;
}

/* Typography improvements for article content */
.post-full-content {
    font-feature-settings: "palt" 1 !important;
    text-align: justify;
    text-justify: inter-character;
}

/* Japanese CJK specific optimizations - enhanced */
.cjk-optimize {
    /* Enable Japanese kerning and spacing features */
    font-feature-settings: "palt" 1, "kern" 1 !important;
    
    /* Fine-tune line breaks for Japanese text */
    line-break: strict;
    
    /* Proper word breaking */
    word-break: normal;
    overflow-wrap: break-word;
    
    /* Optimize Japanese punctuation handling */
    hanging-punctuation: first force-end last !important;
    
    /* Improve spacing */
    text-spacing: trim-start trim-end space-first allow-end !important;
}

/* Japanese number formatting - improved */
.jp-numbers {
    font-feature-settings: "palt" 1, "pkna" 1, "tnum" 1 !important; /* Proportional kana + tabular numbers */
    font-variant-numeric: proportional-nums tabular-nums !important;
}

/* Japanese bold text enhancement */
strong, b, .jp-bold {
    font-weight: var(--jp-font-weight-bold);
    font-feature-settings: "palt" 1;
    letter-spacing: 0.01em;
}

/* Blockquote improvements for Japanese */
blockquote {
    font-size: 0.98em;
    line-height: var(--jp-line-height);
    letter-spacing: 0.01em;
    font-feature-settings: "palt" 1;
    padding-left: 1.2em;
    border-left: 3px solid #e0e0e0;
    color: #505050;
}

/* Responsive font sizing for Japanese text - enhanced */
@media (max-width: 767px) {
    :root {
        font-size: 16px;
        --jp-line-height: 1.9;
        --jp-letter-spacing: 0.01em;
        
        /* Adjust font sizes for mobile */
        --jp-font-size-base: 1rem;
        --jp-font-size-h1: 1.8rem;
        --jp-font-size-h2: 1.5rem;
        --jp-font-size-h3: 1.3rem;
        --jp-font-size-h4: 1.2rem;
        --jp-font-size-h5: 1.1rem;
        --jp-font-size-h6: 1rem;
    }
    
    h1 { 
        letter-spacing: 0.02em;
    }
    h2 { 
        letter-spacing: 0.01em;
    }
    
    p, li, .post-full-content, .gh-content {
        line-height: 1.8;
    }
    
    /* Improve legibility on small screens */
    .post-full-content {
        letter-spacing: 0.01em;
        text-align: left;
    }
}

@media (min-width: 768px) and (max-width: 1199px) {
    :root {
        font-size: 16.5px;
        --jp-line-height: 2.0;
        
        /* Adjust font sizes for tablets */
        --jp-font-size-h1: 2.2rem;
        --jp-font-size-h2: 1.8rem;
        --jp-font-size-h3: 1.5rem;
        --jp-font-size-h4: 1.3rem;
        --jp-font-size-h5: 1.15rem;
        --jp-font-size-h6: 1.05rem;
    }
}

@media (min-width: 1200px) {
    :root {
        font-size: 17px;
        --jp-line-height: 2.1;
        
        /* These match the default values defined in :root */
        --jp-font-size-h1: 2.5rem;
        --jp-font-size-h2: 2rem;
        --jp-font-size-h3: 1.7rem;
        --jp-font-size-h4: 1.4rem;
        --jp-font-size-h5: 1.2rem;
        --jp-font-size-h6: 1.1rem;
    }
    
    /* Enhanced text readability for large screens */
    .post-full-content {
        line-height: 1.95;
    }
}

/* Optimizations for high-DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    
    .jp, .japanese-text, p, li {
        letter-spacing: 0.015em;
    }
}

/* Fix for Firefox font rendering issues with Japanese text */
@-moz-document url-prefix() {
    body {
        font-feature-settings: normal;
    }
    
    .jp, .japanese-text, .cjk-optimize {
        font-feature-settings: normal;
        letter-spacing: 0.01em;
    }
    
    /* Improve Firefox-specific rendering */
    .post-full-content {
        text-rendering: optimizeLegibility;
    }
}

/* Special handling for code blocks and pre elements to use monospace */
code, pre, kbd, samp {
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}

/* Add support for vertical Japanese text */
.jp-vertical {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    font-feature-settings: "vkna" 1, "pkna" 1; /* Vertical kana alternates */
    line-height: 1.7;
    letter-spacing: 0.05em;
}

/* Enhanced legibility for dark mode */
.dark-mode body,
.dark-mode .post-full-content {
    -webkit-font-smoothing: antialiased;
    letter-spacing: 0.025em;
}