/* Color Matrix のスタイル。
   色は道具共通の約束（強調 #2962FF、青に寄せたニュートラル。hub・assets と同じ値）。
   既定はライト。ダークは右上のボタンで切り替え、このブラウザに覚える（app.js）。 */

/* --- トークン --- */
:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --text: #1a1c20;
    --muted: #646a75;
    --border: #dadde3;
    --border-strong: #c5cad3;
    --accent: #2962ff;          /* 白文字とのコントラスト 4.9:1 */
    --accent-hover: #1e4fd8;    /* 押せる面のホバー。白文字はさらに上がる */
    --accent-soft: #e5ecff;
    --accent-text: #1e4fd8;     /* 淡い面・白地の上の文字（4.5:1 以上） */
    --focus: #2962ff;
    --warn: #8a5a00;
    --warn-soft: #fff4dc;
    --ok: #1b7f3b;
    --ok-soft: #e6f4ea;
    --toast-bg: #1a1c20;
    --toast-text: #ffffff;
    --swatch-edge: rgba(0, 0, 0, 0.08); /* 白に近い色見本が背景に溶けないための縁 */
    --scroll-thumb: rgba(0, 0, 0, 0.22);
    --scroll-thumb-hover: rgba(0, 0, 0, 0.35);

    --font-sans: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Segoe UI", sans-serif;
    --font-mono: ui-monospace, "SF Mono", "Osaka-Mono", Menlo, monospace;
    --radius-lg: 14px;
    --radius-md: 8px;
    --radius-sm: 6px;
    --shadow-raise: 0 4px 12px rgba(0, 0, 0, 0.12);
    --ease: 0.15s ease;
    color-scheme: light;
}

[data-theme="dark"] {
    --bg: #15171b;
    --surface: #1d2025;
    --surface-2: #252930;
    --text: #e9ebef;
    --muted: #979ca6;
    --border: #343841;
    --border-strong: #4a4f59;
    --accent: #2962ff;
    --accent-hover: #1e4fd8;
    --accent-soft: #1c2a4d;
    --accent-text: #9db6ff;
    --focus: #85a6ff;           /* 暗い背景では #2962ff の輪郭が沈むので明るくする */
    --warn: #f0c060;
    --warn-soft: #3a2f16;
    --ok: #7fd49a;
    --ok-soft: #183a24;
    --toast-bg: #e9ebef;
    --toast-text: #15171b;
    --swatch-edge: rgba(255, 255, 255, 0.08);
    --scroll-thumb: rgba(255, 255, 255, 0.22);
    --scroll-thumb-hover: rgba(255, 255, 255, 0.35);
    --shadow-raise: 0 4px 12px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
}

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

body {
    font: 14px/1.6 var(--font-sans);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

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

:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
}

/* 見た目は自前で描くが、キーボードとスクリーンリーダーからは使えるように残す */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* --- レイアウト --- */
.app-container {
    max-width: 1540px; /* マトリクスに幅を回すため少し広め */
    margin: 0 auto;
    padding: 32px 24px;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* --- ヘッダー --- */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    margin-bottom: 24px;
    gap: 8px 16px;
}

.app-header h1 {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.3;
}

.app-header h1 small {
    margin-left: 8px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 400;
}

.subtitle {
    color: var(--muted);
    flex-basis: 100%;
    order: 3;
}

/* ライト／ダークの切り替え（lite の .seg と同じ形） */
.theme-seg {
    display: flex;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.theme-seg button {
    border: 0;
    border-right: 1px solid var(--border);
    background: var(--surface);
    color: var(--muted);
    padding: 5px 12px;
    font-size: 13px;
    cursor: pointer;
    transition: color var(--ease), background-color var(--ease);
}

.theme-seg button:last-child {
    border-right: 0;
}

.theme-seg button:hover {
    color: var(--text);
}

.theme-seg button[aria-pressed="true"] {
    background: var(--accent);
    color: #fff;
}

.theme-seg button:focus-visible {
    outline-offset: -2px;
}

/* --- 2カラム --- */
.app-dashboard {
    display: grid;
    grid-template-columns: 340px 1fr;
    gap: 24px;
    align-items: start;
    flex-grow: 1;
}

/* グリッドアイテムの既定は min-width:auto なので、中身（最小1240pxのマトリクス）に
   引きずられてトラックが膨らみ、ページ全体が横に溢れる。0 を明示して縮めるようにし、
   溢れは .matrix-scroll-wrapper の内部スクロールで受ける。 */
.app-dashboard > * {
    min-width: 0;
}

@media (max-width: 1200px) {
    .app-dashboard {
        grid-template-columns: minmax(0, 1fr);
    }
}

@media (max-width: 640px) {
    .app-container {
        padding: 20px 16px;
    }
}

/* --- パネル --- */
.glass-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
}

.section-title {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 4px;
}

/* --- 設定パネル --- */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group > label,
.group-label {
    font-size: 13px;
    font-weight: 600;
}

.label-with-badge {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.badge {
    font-size: 12px;
    font-weight: 600;
    background: var(--accent-soft);
    color: var(--accent-text);
    padding: 2px 8px;
    border-radius: 999px;
}

/* 手動で基準トーンを変えたとき */
.badge.is-manual {
    background: var(--warn-soft);
    color: var(--warn);
}

input[type="text"] {
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    width: 100%;
    transition: border-color var(--ease);
}

input[type="text"]:hover {
    border-color: var(--border-strong);
}

input[type="text"]:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-soft);
}

#color-input {
    font-family: var(--font-mono);
}

.helper-text {
    font-size: 12px;
    color: var(--muted);
    line-height: 1.5;
}

.helper-text code {
    background: var(--surface-2);
    padding: 1px 4px;
    border-radius: 4px;
    font-family: var(--font-mono);
}

.mt-1 { margin-top: 4px; }

/* 色の入力 */
.color-picker-wrapper {
    display: flex;
    gap: 8px;
    align-items: center;
}

.picker-preview {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    overflow: hidden;
    cursor: pointer;
    position: relative;
    flex-shrink: 0;
    transition: border-color var(--ease);
}

.picker-preview:hover {
    border-color: var(--border-strong);
}

.picker-preview input[type="color"] {
    position: absolute;
    top: -10px;
    left: -10px;
    width: 60px;
    height: 60px;
    border: none;
    cursor: pointer;
    background: transparent;
}

/* スライダー */
.slider-wrapper {
    padding: 4px 0;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

.slider-ticks {
    display: flex;
    justify-content: space-between;
    margin-top: 2px;
    padding: 0 2px;
}

.slider-ticks span {
    font-size: 11px;
    color: var(--muted);
    font-family: var(--font-mono);
}

/* ラジオ（生成モード） */
.radio-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    color: var(--text);
}

.radio-label input[type="radio"] {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.radio-custom {
    width: 16px;
    height: 16px;
    border: 1.5px solid var(--border-strong);
    border-radius: 50%;
    flex-shrink: 0;
    position: relative;
    background: var(--surface);
    transition: border-color var(--ease);
}

.radio-label input[type="radio"]:checked + .radio-custom {
    border-color: var(--accent);
}

.radio-custom::after {
    content: '';
    width: 8px;
    height: 8px;
    background: var(--accent);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    border-radius: 50%;
    transition: transform var(--ease);
}

.radio-label input[type="radio"]:checked + .radio-custom::after {
    transform: translate(-50%, -50%) scale(1);
}

.radio-label input[type="radio"]:focus-visible + .radio-custom {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
}

/* --- ボタン --- */
.btn {
    font-weight: 600;
    font-size: 13px;
    padding: 9px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: background-color var(--ease), border-color var(--ease);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

.btn-secondary:hover {
    border-color: var(--border-strong);
}

.action-buttons-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* --- 右側（パレットとコード） --- */
.palette-container {
    display: flex;
    flex-direction: column;
    gap: 24px;
    min-width: 0;
}

.palette-panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 12px;
}

.info-pill {
    background: var(--ok-soft);
    color: var(--ok);
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.pill-dot {
    width: 6px;
    height: 6px;
    background-color: currentColor;
    border-radius: 50%;
    flex-shrink: 0;
}

/* --- 表示の切り替え（単一 / マトリクス） --- */
.view-tabs {
    display: flex;
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 3px;
    border-radius: var(--radius-md);
}

.view-tab-btn {
    font-weight: 600;
    font-size: 13px;
    color: var(--muted);
    background: transparent;
    border: none;
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color var(--ease), background-color var(--ease);
}

.view-tab-btn:hover {
    color: var(--text);
}

.view-tab-btn.active {
    background: var(--accent);
    color: #fff;
}

.palette-view-content {
    display: none;
}

.palette-view-content.active {
    display: block;
}

/* --- 単一カラーパレット --- */
.palette-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 12px;
}

@media (max-width: 640px) {
    .palette-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

.color-card {
    border-radius: var(--radius-md);
    aspect-ratio: 1 / 1.05;
    padding: 14px 12px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 0 0 1px var(--swatch-edge);
    transition: transform var(--ease), box-shadow var(--ease);
    user-select: none;
}

.color-card:hover {
    transform: translateY(-2px);
    box-shadow: inset 0 0 0 1px var(--swatch-edge), var(--shadow-raise);
}

.color-card:active {
    transform: translateY(0);
}

/* 基準色のトーン */
.color-card.is-base-color {
    box-shadow: inset 0 0 0 1px var(--swatch-edge), 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

.color-card.is-base-color::after {
    content: '基準';
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 10px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 4px;
    border: 1px solid currentColor;
    color: inherit;
}

.card-header {
    display: flex;
    flex-direction: column;
}

.color-weight {
    font-size: 18px;
    font-weight: 700;
    line-height: 1.2;
}

.wcag-score {
    font-size: 11px;
    font-weight: 600;
}

.card-footer {
    display: flex;
    flex-direction: column;
}

.color-hex {
    font-family: var(--font-mono);
    font-size: 13px;
}

.wcag-ratio {
    font-size: 11px;
    opacity: 0.8;
}

/* クリックしたときの波紋（コピーした合図） */
.card-ripple {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    width: 150%;
    height: 150%;
    background: currentColor;
    border-radius: 50%;
    pointer-events: none;
    opacity: 0;
}

@keyframes rippleEffect {
    0% { transform: translate(-50%, -50%) scale(0); opacity: 0.2; }
    100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

/* --- フルマトリクス --- */
.matrix-scroll-wrapper {
    overflow-x: auto;
    max-width: 100%;
    margin: 0 -8px;
    /* セルの :hover と基準セルの輪郭が切り取られないよう、上下左右に余白を取る */
    padding: 6px 8px;
    -webkit-overflow-scrolling: touch;
}

.matrix-grid-container {
    min-width: 1240px; /* 19列を潰さないための最小幅 */
}

/* 横スクロールできることが分かるよう、この領域だけスクロールバーを太く・濃くする */
.matrix-scroll-wrapper::-webkit-scrollbar {
    height: 10px;
}

.matrix-scroll-wrapper::-webkit-scrollbar-thumb {
    background: var(--scroll-thumb);
    border-radius: 10px;
}

.matrix-scroll-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--scroll-thumb-hover);
}

/* 20列（行ラベル1 + 色相19） */
.matrix-grid-layout {
    display: grid;
    grid-template-columns: 50px repeat(19, 1fr);
    gap: 4px;
    align-items: center;
}

.matrix-header-cell {
    font-size: 11px;
    font-weight: 600;
    text-align: center;
    padding: 4px 0;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.matrix-row-label {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--muted);
    text-align: right;
    padding-right: 8px;
    user-select: none;
}

.matrix-cell {
    height: 38px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 0 0 1px var(--swatch-edge);
    transition: transform var(--ease), box-shadow var(--ease);
    user-select: none;
    position: relative;
}

.matrix-cell::before {
    content: attr(data-tone);
    font-family: var(--font-mono);
    font-size: 10px;
    opacity: 0.85;
}

/* 入力した色そのもののセル */
.matrix-cell.is-base-cell {
    box-shadow: inset 0 0 0 1px var(--swatch-edge), 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
    z-index: 5;
}

.matrix-cell:hover {
    transform: scale(1.12);
    z-index: 10;
    box-shadow: inset 0 0 0 1px var(--swatch-edge), var(--shadow-raise);
}

.matrix-cell.is-base-cell:hover {
    box-shadow: inset 0 0 0 1px var(--swatch-edge), 0 0 0 2px var(--bg), 0 0 0 4px var(--accent), var(--shadow-raise);
}

/* --- コード --- */
.code-panel {
    display: flex;
    flex-direction: column;
    padding: 0;
    overflow: hidden;
    min-width: 0;
}

.code-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border);
    padding: 0 16px;
    gap: 12px;
}

.code-tabs-header {
    display: flex;
    flex-wrap: wrap;
}

.tab-btn {
    font-weight: 600;
    font-size: 13px;
    color: var(--muted);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    padding: 12px;
    margin-bottom: -1px;
    cursor: pointer;
    transition: color var(--ease);
}

.tab-btn:hover {
    color: var(--text);
}

.tab-btn.active {
    color: var(--accent-text);
    border-bottom-color: var(--accent);
}

/* スイッチ（全19色相を一括出力） */
.code-toggle-wrapper {
    display: flex;
    align-items: center;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

.toggle-label input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.toggle-slider {
    width: 34px;
    height: 20px;
    background-color: var(--border-strong);
    border-radius: 999px;
    position: relative;
    flex-shrink: 0;
    transition: background-color var(--ease);
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider {
    background-color: var(--accent);
}

.toggle-label input[type="checkbox"]:focus-visible + .toggle-slider {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
}

.toggle-slider::before {
    content: '';
    width: 16px;
    height: 16px;
    background: #fff;
    border-radius: 50%;
    position: absolute;
    top: 2px;
    left: 2px;
    transition: transform var(--ease);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.toggle-label input[type="checkbox"]:checked + .toggle-slider::before {
    transform: translateX(14px);
}

.toggle-text {
    font-size: 13px;
    color: var(--muted);
}

.code-body-wrapper {
    position: relative;
    padding: 16px;
    background: var(--surface-2);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

.code-body-wrapper pre {
    margin: 0;
    max-height: 280px;
    overflow: auto; /* 長い行はパネルを広げず、コード内で横スクロールさせる */
}

.code-body-wrapper code {
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.6;
    color: var(--text);
}

.btn-copy-absolute {
    position: absolute;
    top: 12px;
    right: 12px;
    font-size: 12px;
    padding: 5px 10px;
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}

.btn-copy-absolute:hover {
    border-color: var(--border-strong);
}

/* --- フッター --- */
.app-footer-note {
    margin-top: auto;
    padding-top: 32px;
    text-align: center;
}

.app-footer-note p {
    font-size: 12px;
    color: var(--muted);
}

/* --- トースト --- */
.toast-container {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    /* 長い警告文でも画面外へ伸びないよう上限を持たせる */
    max-width: min(640px, calc(100vw - 32px));
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    z-index: 9999;
}

.toast {
    background: var(--toast-bg);
    color: var(--toast-text);
    padding: 10px 16px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-raise);
    line-height: 1.6;
    opacity: 0;
    animation: toastIn 0.2s ease forwards, toastOut 0.3s ease forwards 2.7s;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes toastOut {
    to { opacity: 0; visibility: hidden; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* unitcell tools の入口へ戻るリンク（道具をまたいで同じ形。文字色は周りから継ぐ） */
.ut-home { color: inherit; opacity: 0.6; font-size: 12px; font-weight: 400; text-decoration: none; white-space: nowrap; }
.ut-home::after { content: " /"; }
.ut-home:hover { opacity: 1; text-decoration: underline; }
