/* --- CONFIGURATION & VARIABLES --- */
:root {
    --ios-bg: rgba(255, 255, 255, 0.95);
    --ios-shadow: 0 15px 50px rgba(0, 0, 0, 0.15);
    --cell-width: clamp(44px, calc((100vw - 64px) / 6), 112px);
    --cell-height: clamp(36px, calc(var(--cell-width) * 0.5714), 64px);
    --grid-line: 1px;
    --motion-press: 90ms;
    --motion-fast: 140ms;
    --motion-base: 180ms;
    --motion-exit: 120ms;
    --ease-enter: cubic-bezier(0.2, 0.8, 0.2, 1);
    --ease-exit: cubic-bezier(0.4, 0, 1, 1);
    --ease-standard: cubic-bezier(0.2, 0, 0, 1);
    --color-1: #ffadad;
    --color-1-a: rgba(255, 173, 173, 0.5);
    --color-2: #ffd6a5;
    --color-2-a: rgba(255, 214, 165, 0.5);
    --color-3: #fdffb6;
    --color-3-a: rgba(253, 255, 182, 0.5);
    --color-4: #caffbf;
    --color-4-a: rgba(202, 255, 191, 0.5);
    --color-5: #9bf6ff;
    --color-5-a: rgba(155, 246, 255, 0.5);
}

/* --- GLOBAL STYLES --- */
* {
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-user-drag: none;
}

input, textarea {
    -webkit-user-select: text;
    -ms-user-select: text;
    user-select: text;
    cursor: auto;
}

.visually-hidden {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

button,
a,
.timestamp-toggle,
.legend-item,
.circle-display,
.editable-label,
.paintable,
.paintable-name,
.menu-option,
.menu-reset,
.menu-icon-action,
.menu-delete-group,
.restore-deleted-button,
.restore-group-item-button {
    touch-action: manipulation;
}

body {
    font-family:
        "Pretendard Variable",
        Pretendard,
        -apple-system,
        BlinkMacSystemFont,
        system-ui,
        Roboto,
        "Helvetica Neue",
        "Segoe UI",
        "Apple SD Gothic Neo",
        "Noto Sans KR",
        "Malgun Gothic",
        "Apple Color Emoji",
        "Segoe UI Emoji",
        "Segoe UI Symbol",
        sans-serif;
    display: flex; justify-content: center; align-items: flex-start;
    min-height: 100vh; margin: 0; padding-top: clamp(32px, 5vh, 64px); box-sizing: border-box;
    /* background-color: #f2f2f7; */
}

/* --- MAIN CONTAINER --- */
.container {
    max-width: calc(var(--cell-width) * 6 + 80px);
    width: 100%; box-sizing: border-box;
    background-color: #ffffff; padding: 0 clamp(16px, 4vw, 32px) 48px; margin: 0;
    text-align: center; position: relative;
}
h1 { font-size: clamp(16px, 2.6vw, 22px); font-weight: 600; letter-spacing: -0.5px; color: #1d1d1f; }
.page-header {
    display: grid; grid-template-columns: 40px minmax(0, 1fr) 40px; align-items: center;
    margin: clamp(24px, 4vw, 32px) 0; position: relative; z-index: 20;
}
.page-header h1 { grid-column: 2; margin: 0; }
.page-header-spacer { grid-column: 1; }
.language-control {
    position: absolute; top: 50%; right: 0; transform: translateY(-50%);
}
.language-button {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; padding: 0; border: 1px solid #d1d1d6; border-radius: 10px;
    background: #fff; color: #636366; cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.language-button svg {
    width: 19px; height: 19px; stroke: currentColor; stroke-width: 1.5;
    stroke-linecap: round; stroke-linejoin: round;
}
.language-button:active { background: #f2f2f7; transform: scale(0.96); }
.language-button:focus-visible,
.language-option:focus-visible,
.language-request:focus-visible,
.contact-action:focus-visible { outline: 3px solid rgba(0, 122, 255, 0.24); outline-offset: 2px; }
.language-menu {
    position: absolute; top: calc(100% + 8px); right: 0; z-index: 30;
    display: grid; min-width: 142px; padding: 6px;
    background: var(--ios-bg); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 12px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.14); backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
}
.language-menu[hidden] { display: none; }
.language-option,
.language-request {
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    min-height: 38px; padding: 0 10px; border: 0; border-radius: 8px;
    background: transparent; color: #1c1c1e; font-family: inherit; font-size: 14px;
    text-align: left; cursor: pointer;
}
.language-option:active,
.language-request:active { background: #e5e5ea; }
.language-option-check { width: 14px; color: #007aff; font-size: 14px; font-weight: 700; }
.language-option[aria-checked="true"] .language-option-check::after { content: '✓'; }
.language-menu-separator { height: 1px; margin: 5px 6px; background: #e5e5ea; }
.language-request { justify-content: flex-start; gap: 7px; color: #0066cc; font-weight: 500; }
.language-request::before { content: '+'; font-size: 17px; font-weight: 400; line-height: 1; }

/* --- LEGEND SECTION --- */
.legend-wrapper { display: flex; justify-content: center; align-items: center; gap: clamp(10px, 2.5vw, 20px); margin-bottom: clamp(28px, 5vw, 40px); flex-wrap: wrap; }
.is-restoring-chart-state .legend-wrapper { visibility: hidden; }
.is-restoring-chart-state .chart-frame { visibility: hidden; }
.legend-item {
    display: flex; align-items: center; gap: clamp(6px, 1.25vw, 10px); position: relative;
    opacity: 1; transform: translateY(0) scale(1);
    transition: opacity var(--motion-base) var(--ease-enter), transform var(--motion-base) var(--ease-enter);
}
.legend-item.is-entering { opacity: 0; transform: translateY(3px) scale(0.98); }
.legend-item.is-leaving {
    opacity: 0; transform: translateY(-2px) scale(0.97);
    pointer-events: none;
    transition-duration: var(--motion-exit);
    transition-timing-function: var(--ease-exit);
}
.circle-display {
    width: clamp(16px, 2.8vw, 24px); height: clamp(16px, 2.8vw, 24px);
    border: var(--grid-line) solid rgba(0,0,0,0.1); border-radius: 50%; cursor: pointer;
    transition: transform var(--motion-fast) var(--ease-standard);
}
.circle-display:active { transform: scale(0.94); }
.editable-label {
    font-size: clamp(12px, 2.2vw, 16px); font-weight: 500; cursor: pointer; color: #3a3a3c;
    transition: color var(--motion-fast) var(--ease-standard);
}
.editable-label:active { color: #8e8e93; }
.btn-delete-item {
    position: absolute; top: -8px; right: -10px; width: 20px; height: 20px;
    border-radius: 50%; background: #ff453a; color: white; border: none; font-size: 10px;
    cursor: pointer; display: flex; align-items: center; justify-content: center; z-index: 5;
    opacity: 0; visibility: hidden; pointer-events: none; transform: scale(0.85);
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        opacity var(--motion-fast) var(--ease-standard),
        transform var(--motion-fast) var(--ease-enter),
        visibility 0s linear var(--motion-fast);
}
.btn-delete-item:active { transform: scale(0.82); }
.btn-add-legend {
    width: clamp(16px, 2.8vw, 24px); height: clamp(16px, 2.8vw, 24px);
    border: var(--grid-line) dashed #c7c7cc; border-radius: 50%; background: none; cursor: pointer;
    font-size: clamp(12px, 2.2vw, 20px); color: #8e8e93; margin-left: 5px;
    display: flex; align-items: center; justify-content: center;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.btn-add-legend:active { transform: scale(0.9); }

/* --- TABLE SECTION --- */
.chart-frame {
    --visible-table-column-count: 6;
    position: relative;
    width: calc(var(--cell-width) * var(--visible-table-column-count));
    min-height: calc(var(--cell-height) * 6);
    margin: 0 auto;
}
.table-shell{ width: 100%; margin: 0 auto; box-sizing: border-box; border: 0; border-radius: 12px; overflow: hidden; background: #fff; }
#rpsTable{ width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; border: 0; border-radius: 0; overflow: visible; background-color: #fff; }
#rpsTable th, #rpsTable td { width: var(--cell-width); border: 0; height: var(--cell-height); text-align: center; font-size: clamp(10px, 2vw, 16px); font-weight: 400; box-sizing: border-box; background-clip: border-box; }
#rpsTable tr > *:first-child{ background-color: #636366; color: white; font-weight: 500; }
#rpsTable thead tr:first-child > *{ background-color: #636366; color: white; font-weight: 500; }
.table-grid-overlay {
    position: absolute; inset: 0; z-index: 1; display: block;
    width: 100%; height: 100%; overflow: visible; pointer-events: none;
}
.paintable, .paintable-name {
    cursor: pointer;
    transition:
        background-color var(--motion-base) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard);
    background-color: transparent;
}
.paintable.is-ghost { color: #fff; }
.is-adjusting-color .paintable { transition-duration: 0s; }

/* --- IMAGE EXPORT CONTROLS --- */
.chart-timestamp {
    position: absolute; top: calc(100% + 10px); right: 0;
    color: #8e8e93; font-size: 13px; font-variant-numeric: tabular-nums;
}
.export-panel {
    display: flex; flex-direction: column; gap: 12px;
    width: min(220px, 84vw); box-sizing: border-box; margin: clamp(44px, 7vw, 54px) auto 0;
}
.timestamp-toggle {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    color: #636366; font-size: 13px; font-weight: 400; cursor: pointer;
}
.timestamp-toggle-input {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.timestamp-toggle-control {
    position: relative; flex: 0 0 auto; width: 18px; height: 18px;
    box-sizing: border-box; border: 1px solid #c7c7cc; border-radius: 5px;
    background: #fff;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.timestamp-toggle-control::after {
    content: ''; position: absolute; left: 5px; top: 2px; width: 5px; height: 9px;
    border: solid #fff; border-width: 0 1.5px 1.5px 0; transform: rotate(45deg) scale(0);
    transition: transform var(--motion-fast) var(--ease-standard);
}
.timestamp-toggle-input:checked + .timestamp-toggle-control { background: #636366; border-color: #636366; }
.timestamp-toggle-input:checked + .timestamp-toggle-control::after { transform: rotate(45deg) scale(1); }
/* The native checkbox is visually clipped, so its visible proxy keeps browser focus discoverable. */
.timestamp-toggle-input:focus-visible + .timestamp-toggle-control { outline: 3px solid rgba(99, 99, 102, 0.18); outline-offset: 2px; }
.timestamp-toggle:active .timestamp-toggle-control { transform: scale(0.94); }
.btn-save-image {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    width: 100%; min-height: 40px; padding: 9px 16px; border: 1px solid #d1d1d6; border-radius: 10px;
    background: #fff; color: #3a3a3c; font-family: inherit; font-size: 14px; font-weight: 500;
    cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        opacity var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.btn-save-image svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.btn-save-image:active { transform: scale(0.985); background: #e5e5ea; }
.btn-save-image:disabled { color: #8e8e93; background: #f2f2f7; cursor: wait; opacity: 0.8; transform: none; }
.btn-save-image.is-success:disabled {
    color: #248a3d; background: #eefaf1; border-color: #b7e4c0;
    cursor: default; opacity: 1;
}

.history-controls {
    display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 8px;
    margin-top: 16px;
}
.history-button {
    display: inline-flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; padding: 0; border: 1px solid #d1d1d6; border-radius: 10px;
    background: #fff; color: #636366; cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        opacity var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.history-button svg {
    width: 17px; height: 17px; stroke: currentColor; stroke-width: 1.6;
    stroke-linecap: round; stroke-linejoin: round;
}
.history-button:active { background: #e5e5ea; transform: scale(0.95); }
.history-button:disabled {
    background: #fff; border-color: #e5e5ea; color: #c7c7cc;
    cursor: default; opacity: 0.7; transform: none;
}
.reset-button,
.restore-deleted-button {
    min-height: 34px; padding: 0 12px; border: 1px solid #d1d1d6; border-radius: 10px;
    background: #fff; color: #636366; font-family: inherit; font-size: 12px; font-weight: 500;
    cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.restore-deleted-button {
    position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    white-space: nowrap;
}
.restore-deleted-button:active { background: #eef5ff; transform: scale(0.97); }
.restore-deleted-button:disabled {
    background: #fff; border-color: #e5e5ea; color: #c7c7cc;
    cursor: default; opacity: 0.7; transform: none;
}
.restore-deleted-button:focus-visible,
.restore-group-item-button:focus-visible {
    outline: 3px solid rgba(0, 122, 255, 0.24); outline-offset: 2px;
}
.restore-deleted-count {
    min-width: 17px; height: 17px; box-sizing: border-box; padding: 0 5px;
    border-radius: 999px; background: #0066cc; color: #fff;
    font-size: 10px; font-weight: 600; line-height: 17px; text-align: center;
}
.reset-button:active { background: #ffe5e3; transform: scale(0.97); }

.image-export-frame {
    position: fixed; top: 0; left: -10000px; z-index: -1;
    box-sizing: content-box; overflow: visible; background: #fff; text-align: center;
    --cell-width: 69.5px;
    --cell-height: 39.75px;
    --grid-line: 1px;
}
.image-export-content { position: relative; box-sizing: border-box; }
.image-export-frame h1 { font-size: 16px; margin: 0 0 24px; }
.image-export-frame .legend-wrapper { gap: 12px; margin-bottom: 28px; }
.image-export-frame .legend-item { gap: 6px; }
.image-export-frame .circle-display { width: 16px; height: 16px; }
.image-export-frame .editable-label { font-size: 12px; }
.image-export-frame .chart-frame {
    width: calc(var(--cell-width) * var(--visible-table-column-count));
    min-height: 0;
}
.image-export-frame .table-grid-overlay { display: none; }
.image-export-frame #rpsTable th,
.image-export-frame #rpsTable td { font-size: 10px; }
.image-export-frame .chart-timestamp { top: calc(100% + 8px); font-size: 10px; }
.image-export-frame .btn-add-legend,
.image-export-frame .btn-delete-item { display: none !important; }
.image-export-frame .circle-display:hover { transform: none; }
.image-export-frame .editable-label:hover { color: #3a3a3c; text-decoration: none; }

/* --- FOOTER --- */
.site-footer {
    display: flex; justify-content: center; align-items: center; flex-wrap: wrap;
    gap: clamp(6px, 1vw, 8px); margin-top: clamp(56px, 8vw, 64px); color: #8e8e93; font-size: clamp(10px, 2.4vw, 13px);
}
.site-footer-trigger {
    padding: 0; border: 0; background: transparent; font: inherit; cursor: pointer;
    color: inherit; text-decoration-color: #c7c7cc; text-underline-offset: 3px;
    text-decoration-line: underline;
    transition: color var(--motion-fast) var(--ease-standard);
}

/* --- POPUP (iOS STYLE) --- */
.ios-popup {
    display: none; position: absolute; background: var(--ios-bg);
    backdrop-filter: blur(25px); -webkit-backdrop-filter: blur(25px);
    border-radius: 24px; box-shadow: var(--ios-shadow); padding: 16px;
    z-index: 1000; border: 0.5px solid rgba(255, 255, 255, 0.5);
    flex-direction: column; width: 220px;
    opacity: 0; pointer-events: none; transform: translateY(-4px) scale(0.98);
    transform-origin: center;
    transition:
        opacity var(--motion-fast) var(--ease-enter),
        transform var(--motion-fast) var(--ease-enter);
}
.ios-popup.is-open { opacity: 1; pointer-events: auto; transform: translateY(0) scale(1); }
.ios-popup.is-closing {
    opacity: 0; pointer-events: none; transform: translateY(-2px) scale(0.99);
    transition-duration: var(--motion-exit);
    transition-timing-function: var(--ease-exit);
}
.ios-popup::before { content: ''; position: absolute; top: -8px; left: 50%; transform: translateX(-50%); border-left: 8px solid transparent; border-right: 8px solid transparent; border-bottom: 8px solid var(--ios-bg); }

#cellMenu { width: max-content; flex-direction: row; align-items: center; gap: 10px; }
#cellMenu::before { top: auto; bottom: -8px; left: var(--arrow-left, 50%); border-bottom: none; border-top: 8px solid var(--ios-bg); }
.menu-option {
    width: 34px; height: 34px; border-radius: 50%; border: 1px solid rgba(0,0,0,0.05);
    cursor: pointer;
    transition: transform var(--motion-press) var(--ease-standard);
}
.menu-reset {
    width: 34px; height: 34px; border-radius: 50%; background: #e5e5ea;
    display: flex; align-items: center; justify-content: center; cursor: pointer;
    font-size: 18px; color: #8e8e93;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.menu-option:active { transform: scale(0.9); }
.menu-reset:active { transform: scale(0.9); background: #d1d1d6; }
.menu-divider {
    width: 1px; height: 26px; flex: 0 0 auto; margin: 0 1px;
    border-radius: 999px; background: #d1d1d6;
}
.menu-icon-action,
.menu-delete-group {
    height: 34px; box-sizing: border-box; border: 0; border-radius: 10px;
    font-family: inherit; cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.menu-icon-action {
    display: inline-flex; align-items: center; justify-content: center;
    width: 34px; flex: 0 0 34px; padding: 0;
    background: #e5e5ea; color: #636366;
}
.menu-icon-action svg {
    width: 19px; height: 19px; stroke: currentColor; stroke-width: 1.65;
    stroke-linecap: round; stroke-linejoin: round;
}
.menu-icon-action .visibility-slash {
    stroke: currentColor; stroke-width: 1.65;
}
.menu-icon-action .visibility-slash-backdrop { stroke: #e5e5ea; stroke-width: 4.5; }
.menu-icon-action.is-active .visibility-slash-backdrop { stroke: #d1d1d6; }
.menu-icon-action.is-active { background: #d1d1d6; color: #3a3a3c; }
.menu-delete-group {
    flex: 0 0 auto; padding: 0 11px; white-space: nowrap;
    background: #fff0ef; color: #d70015; font-size: 12px; font-weight: 600;
}
.menu-icon-action:active,
.menu-delete-group:active { transform: scale(0.92); }
.menu-icon-action:focus-visible,
.menu-delete-group:focus-visible {
    outline: 3px solid rgba(0, 122, 255, 0.24); outline-offset: 2px;
}

/* --- COLOR PICKER --- */
.picker-container { display: flex; flex-direction: column; gap: 12px; }
.sat-bright-area { width: 100%; height: 150px; border-radius: 12px; position: relative; cursor: crosshair; touch-action: none; overflow: hidden; background-image: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, transparent); background-color: red; }
.picker-cursor { position: absolute; width: 12px; height: 12px; border: 2px solid white; border-radius: 50%; transform: translate(-50%, -50%); pointer-events: none; box-shadow: 0 0 4px rgba(0,0,0,0.5); }
.hue-slider,
.alpha-slider {
    width: 100%; height: 32px; margin: 0; padding: 0;
    appearance: none; -webkit-appearance: none; background: transparent;
    cursor: pointer;
}
.hue-slider {
    --slider-track-background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
}
.alpha-slider {
    --slider-track-background: repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 0 0 / 14px 14px;
}
.hue-slider::-webkit-slider-runnable-track,
.alpha-slider::-webkit-slider-runnable-track {
    height: 6px; border: 0; border-radius: 999px;
    background: var(--slider-track-background);
}
.hue-slider::-moz-range-track,
.alpha-slider::-moz-range-track {
    height: 6px; border: 0; border-radius: 999px;
    background: var(--slider-track-background);
}
.hue-slider::-webkit-slider-thumb,
.alpha-slider::-webkit-slider-thumb {
    appearance: none; -webkit-appearance: none;
    width: 22px; height: 22px; margin-top: -8px;
    background: white; border: 1px solid #c7c7cc; border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0,0,0,0.24);
}
.hue-slider::-moz-range-thumb,
.alpha-slider::-moz-range-thumb {
    width: 22px; height: 22px;
    background: white; border: 1px solid #c7c7cc; border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0,0,0,0.24);
}
.picker-label-row {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.selected-color-preview {
    display: flex; align-items: center; justify-content: flex-end; gap: 7px;
    min-height: 24px; color: #636366; font-size: 11px; font-variant-numeric: tabular-nums;
}
.selected-color-label { margin-right: auto; }
.selected-color-chip {
    display: inline-flex; width: 24px; height: 24px; flex: 0 0 auto; padding: 0;
    border: 1px solid #d1d1d6; border-radius: 50%; overflow: hidden;
    background: repeating-conic-gradient(#d1d1d6 0% 25%, #fff 0% 50%) 0 0 / 8px 8px;
}
.selected-color-swatch { width: 100%; height: 100%; }
.btn-done {
    background: #007aff; color: white; border: none; padding: 10px;
    border-radius: 12px; font-size: 15px; font-weight: 600; cursor: pointer; margin-top: 5px;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.btn-done:active { transform: scale(0.97); }

/* --- MODAL SYSTEM --- */
.modal-overlay {
    display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.4); justify-content: center; align-items: center;
    z-index: 2000; opacity: 0; pointer-events: none;
    transition: opacity var(--motion-fast) var(--ease-enter);
}
.modal-overlay.is-open { opacity: 1; pointer-events: auto; }
.modal-overlay.is-closing {
    opacity: 0; pointer-events: none;
    transition-duration: var(--motion-exit);
    transition-timing-function: var(--ease-exit);
}
.modal {
    background: white; padding: 25px; border-radius: 24px;
    width: 300px; box-shadow: var(--ios-shadow); text-align: left;
    opacity: 0; transform: translateY(4px) scale(0.98);
    transition:
        opacity var(--motion-base) var(--ease-enter),
        transform var(--motion-base) var(--ease-enter);
}
.modal-overlay.is-open .modal { opacity: 1; transform: translateY(0) scale(1); }
.modal-overlay.is-closing .modal {
    opacity: 0; transform: translateY(2px) scale(0.99);
    transition-duration: var(--motion-exit);
    transition-timing-function: var(--ease-exit);
}
.modal:not(.unified-modal) {
    box-sizing: border-box;
    width: min(350px, calc(100vw - 32px));
}
.modal h3 {
    margin-top: 0;
    color: #1c1c1e;
    font-size: 16px;
    font-weight: 600;
    line-height: 1.2;
}
.modal p { color: #8e8e93; margin-bottom: 20px; font-size: 13px; line-height: 1.5; }
.modal input { width: 100%; padding: 12px; margin: 15px 0; border: 1px solid #d1d1d6; border-radius: 10px; box-sizing: border-box; font-size: 16px; }
.modal .legend-name-input {
    transition:
        border-color var(--motion-fast) var(--ease-standard),
        box-shadow var(--motion-fast) var(--ease-standard);
}
.modal .legend-name-input.has-error,
.unified-modal .input-group .legend-name-input.has-error {
    border: 1px solid #ff453a; box-shadow: 0 0 0 3px rgba(255, 69, 58, 0.14);
}
.modal .legend-name-input.is-limit-hit { animation: nameLimitHit var(--motion-base) ease-in-out; }
.modal .legend-name-error {
    height: 17px; margin: -8px 0 15px; overflow: hidden;
    color: #d70015; font-size: 12px; line-height: 17px;
    opacity: 0; visibility: hidden; pointer-events: none;
    transition:
        opacity var(--motion-fast) var(--ease-standard),
        visibility 0s linear var(--motion-fast);
}
.modal .legend-name-error.is-visible {
    opacity: 1; visibility: visible;
    transition-delay: 0s;
}
.unified-modal {
    width: 75%;
    max-width: 340px;
    padding: 20px;
    border-radius: 16px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}
.unified-modal h3 {
    text-align: center;
    margin-top: 0;
    margin-bottom: 16px;
    font-size: 15px;
    font-weight: 600;
}
.unified-modal .input-group { margin-bottom: 10px; }
.unified-modal .input-group label,
.unified-modal .picker-section-label {
    display: block;
    font-size: 12px;
    font-weight: 400;
    color: #8e8e93;
    margin-bottom: 0;
    text-align: left;
}
.unified-modal .input-group input {
    width: 100%;
    padding: 12px 14px;
    background-color: #f2f2f7;
    border: 1px solid transparent;
    border-radius: 12px;
    font-size: 16px;
    box-sizing: border-box;
    margin: 6px 0 4px;
}
.unified-modal .input-group .legend-name-error {
    margin: 0;
    text-align: left;
}
.unified-modal .picker-container { gap: 6px; }
.unified-modal .sat-bright-area { height: 12dvh; }
#unifiedHueSlider,
#unifiedAlphaSlider {
    height: 44px;
    margin: 0;
}
#unifiedHueSlider::-webkit-slider-runnable-track,
#unifiedAlphaSlider::-webkit-slider-runnable-track { height: 4px; }
#unifiedHueSlider::-moz-range-track,
#unifiedAlphaSlider::-moz-range-track { height: 4px; }
#unifiedHueSlider::-webkit-slider-thumb,
#unifiedAlphaSlider::-webkit-slider-thumb {
    width: 28px;
    height: 28px;
    margin-top: -12px;
}
#unifiedHueSlider::-moz-range-thumb,
#unifiedAlphaSlider::-moz-range-thumb {
    width: 28px;
    height: 28px;
}
.unified-modal .btn-cancel,
.unified-modal .btn-save,
.unified-modal .btn-delete-text {
    height: 44px;
    font-size: 14px;
    font-weight: 500;
    border-radius: 10px;
    border: none;
}
.unified-modal .btn-delete-text {
    width: 100%;
    color: #ff453a;
    background: #ffecec;
}
.unified-action-buttons {
    display: flex;
    flex-direction: column-reverse;
    gap: 10px;
    margin-top: 16px;
}
.unified-action-buttons .right-buttons {
    display: flex;
    width: 100%;
    gap: 10px;
}
.unified-action-buttons .right-buttons button { flex: 1; }
@keyframes nameLimitHit {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-3px); }
}
.modal-buttons { display: flex; justify-content: flex-end; gap: 10px; }
.modal-buttons button,
.unified-action-buttons button {
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.modal-buttons button {
    padding: 12px 20px; border-radius: 12px; border: none;
    cursor: pointer; font-weight: 600; font-size: 13px;
}
.modal-buttons button:active,
.unified-action-buttons button:active { transform: scale(0.97); }
.btn-save { background: #007aff; color: white; }
.btn-cancel { background: #f2f2f7; color: #3a3a3c; }
.btn-delete { background: #ff453a !important; color: white; }

.restore-deleted-modal { max-height: min(640px, calc(100dvh - 32px)); }
#restoreDeletedModalDescription { margin: 0 0 16px; color: #636366; font-size: 13px; line-height: 1.45; }
.restore-deleted-list {
    display: flex; flex-direction: column; gap: 18px;
    max-height: min(380px, calc(100dvh - 270px)); overflow-y: auto;
    margin: 0 -6px 20px; padding: 0 6px;
    overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
.restore-group-section h4 {
    margin: 0 0 8px; color: #636366; font-size: 12px; font-weight: 600;
}
.restore-group-items { display: flex; flex-direction: column; gap: 8px; }
.restore-group-item {
    display: flex; align-items: center; gap: 10px; min-height: 44px;
}
.restore-group-name {
    display: flex; flex: 1; align-items: center; min-width: 0; min-height: 44px;
    padding: 0 12px; border: 1px solid #e5e5ea; border-radius: 10px;
    background: #f2f2f7; color: #1c1c1e; font-size: 13px; font-weight: 500;
}
.restore-group-item-button {
    flex: 0 0 auto; align-self: center; min-width: 64px; min-height: 44px; padding: 0 14px;
    border: 1px solid #b8d7ff; border-radius: 10px; background: #fff; color: #0066cc;
    font-family: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.restore-group-item-button:active { background: #e8f2ff; transform: scale(0.96); }
.restore-groups-empty { margin: 8px 0 !important; color: #636366 !important; text-align: center; }

.contact-modal {
    max-height: calc(100dvh - 32px); overflow-y: auto;
}
.contact-modal h3 { margin-bottom: 18px; }
.contact-sections { display: grid; gap: 18px; }
.contact-section + .contact-section {
    padding-top: 18px; border-top: 1px solid #e5e5ea;
}
.contact-section h4 {
    margin: 0 0 7px; color: #3a3a3c; font-size: 13px; font-weight: 600; line-height: 1.4;
}
.is-localization-entry .contact-section-localization h4 { color: #0066cc; }
.contact-section p { margin: 0 0 12px; color: #636366; }
.contact-action {
    display: flex; align-items: center; justify-content: center; box-sizing: border-box;
    width: 100%; min-height: 40px; padding: 8px 14px; border: 1px solid #d1d1d6;
    border-radius: 10px; background: #fff; color: #0066cc; font-size: 13px;
    font-weight: 600; text-decoration: none;
    transition:
        background-color var(--motion-fast) var(--ease-standard),
        border-color var(--motion-fast) var(--ease-standard),
        transform var(--motion-press) var(--ease-standard);
}
.contact-action:active { background: #eef5ff; transform: scale(0.98); }
.contact-modal-actions { margin-top: 20px; }
.restore-deleted-actions { padding-top: 2px; }
.restore-deleted-actions button:disabled { opacity: 0.45; cursor: default; }

@media (hover: hover) and (pointer: fine) {
    .language-button:hover,
    .language-option:hover { background: #f2f2f7; border-color: #c7c7cc; color: #3a3a3c; }
    .language-request:hover { background: #f2f2f7; border-color: #c7c7cc; color: #0066cc; }
    .circle-display:hover { transform: scale(1.12); }
    .editable-label:hover { color: #8e8e93; text-decoration: underline; }
    .legend-item:hover .btn-delete-item {
        opacity: 1; visibility: visible; pointer-events: auto; transform: scale(1);
        transition-delay: 0s;
    }
    .btn-delete-item:hover { background: #e6352d; }
    .btn-add-legend:hover { background: #f2f2f7; border-color: #8e8e93; color: #636366; }
    .menu-option:hover { transform: scale(1.08); }
    .menu-reset:hover { background: #d1d1d6; color: #636366; transform: scale(1.06); }
    .menu-icon-action:hover { background: #d1d1d6; color: #3a3a3c; transform: scale(1.06); }
    .menu-delete-group:hover { background: #ffd9d6; color: #b50012; }
    .btn-save-image:hover { background: #f2f2f7; border-color: #c7c7cc; }
    .history-button:hover { background: #f2f2f7; border-color: #c7c7cc; color: #3a3a3c; }
    .reset-button:hover { background: #fff1f0; border-color: #ffc9c5; color: #d70015; }
    .restore-deleted-button:not(:disabled):hover { background: #eef5ff; border-color: #b8d7ff; color: #0066cc; }
    .restore-group-item-button:hover { background: #d8eaff; }
    .btn-done:hover,
    .btn-save:hover { background: #006ee6; }
    .btn-cancel:hover { background: #e5e5ea; }
    .btn-delete:hover { background: #e6352d !important; }
    .btn-delete-text:hover { background: #ffd9d6; }
    .site-footer-trigger:hover { color: #3a3a3c; }
    .contact-action:hover { background: #eef5ff; border-color: #b8d7ff; }
}

/* --- Intermediate screens --- */
@media (min-width: 481px) and (max-width: 1024px) {
    #cellMenu {
        gap: 8px; padding: 10px 12px;
        max-width: calc(100vw - 24px); box-sizing: border-box;
        flex-wrap: wrap; justify-content: center; border-radius: 20px;
    }
    .menu-option,
    .menu-reset { width: 26px; height: 26px; font-size: 15px; }
    .menu-divider { height: 20px; }
    .menu-icon-action { width: 26px; height: 26px; flex-basis: 26px; }
    .menu-icon-action svg { width: 16px; height: 16px; }
    .menu-delete-group { height: 26px; padding: 0 8px; font-size: 10px; }
}

/* --- Mobile --- */
@media (max-width: 480px) {
    body {
        min-height: 100dvh;
        padding-top: clamp(32px, 5dvh, 64px);
    }
    .container {
        padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
    }
    h1 {
        margin: 0;
    }
    .page-header { margin: clamp(24px, 4vw, 32px) 0; }
    .legend-wrapper {
        gap: clamp(10px, 2.5vw, 20px);
        margin-bottom: clamp(28px, 5vw, 40px);
    }
    .legend-item {
        gap: clamp(6px, 1.25vw, 10px);
    }
    .site-footer {
        gap: clamp(6px, 1vw, 8px);
        margin-top: clamp(56px, 8vw, 64px);
    }
    .chart-timestamp { top: calc(100% + 8px); font-size: clamp(10px, 2.4vw, 13px); }
    .export-panel {
        margin-top: clamp(44px, 7vw, 54px); gap: 10px;
    }
    .timestamp-toggle { font-size: 12px; }
    .btn-save-image { min-height: 38px; font-size: 13px; }
    .history-controls { margin-top: 14px; }
    .history-button { width: 44px; height: 44px; border-radius: 10px; }
    .reset-button,
    .restore-deleted-button { min-height: 44px; padding: 0 12px; border-radius: 10px; font-size: 12px; }
    .circle-display {
        width: clamp(16px, 2.8vw, 24px);
        height: clamp(16px, 2.8vw, 24px);
    }
    .editable-label {
        font-size: clamp(12px, 2.2vw, 16px);
    }
    .btn-add-legend {
        width: clamp(16px, 2.8vw, 24px);
        height: clamp(16px, 2.8vw, 24px);
        border: var(--grid-line) dashed #c7c7cc;
        font-size: 10px;
        margin-left: 5px;
    }
    .table-shell {
        border-radius: 12px;
        border: 0;
    }
    #rpsTable th, #rpsTable td {
        font-size: clamp(10px, 2vw, 16px);
        font-weight: 400;
        border: 0;
    }
    #rpsTable tr > *:first-child {
        font-weight: 500;
    }
    #rpsTable thead tr:first-child > * {
        font-weight: 500;
    }
    #modalTitle {
        text-align: center;
        margin-top: 0;
        margin-bottom: 16px;
        font-size: 16px;
        font-weight: 600;
    }
    .ios-popup {
        border-radius: 16px;
    }
    #cellMenu {
        gap: 6px;
        padding: 8px 10px;
        max-width: calc(100vw - 16px);
        box-sizing: border-box;
        flex-wrap: wrap;
        justify-content: center;
    }
    .menu-option,
    .menu-reset {
        width: 20px; height: 20px;
        font-size: 12px;
    }
    .menu-divider { height: 20px; margin: 0; }
    .menu-icon-action { width: 28px; height: 28px; flex-basis: 28px; border-radius: 8px; }
    .menu-icon-action svg { width: 16px; height: 16px; }
    .menu-delete-group { height: 28px; padding: 0 8px; border-radius: 8px; font-size: 10px; }
}

@media (max-width: 350px) {
    .unified-modal .legend-name-error { height: 34px; }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        transition-delay: 0s !important;
        scroll-behavior: auto !important;
    }

    .circle-display:hover,
    .circle-display:active,
    .btn-delete-item:hover,
    .btn-delete-item:active,
    .btn-add-legend:hover,
    .btn-add-legend:active,
    .menu-option:hover,
    .menu-option:active,
    .menu-reset:hover,
    .menu-reset:active,
    .menu-icon-action:hover,
    .menu-icon-action:active,
    .menu-delete-group:hover,
    .menu-delete-group:active,
    .btn-save-image:active,
    .history-button:active,
    .reset-button:active,
    .restore-deleted-button:active,
    .restore-group-item-button:active,
    .modal-buttons button:active,
    .unified-action-buttons button:active {
        transform: none !important;
    }
}
