/* ===== 모바일 전용 CSS ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #8b5cf6;
    --accent: #ec4899;
    --bg-dark: #0f172a;
    --bg-medium: #1e293b;
    --bg-light: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border-color: #475569;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --user-color: #3b82f6;
    --ai-color: #ef4444;
    --gold: #fbbf24;
    --light-square: #f0d9b5;
    --dark-square: #b58863;
    --highlight-move: rgba(16, 185, 129, 0.55);
    --highlight-select: rgba(99, 102, 241, 0.6);
    --highlight-last: rgba(251, 191, 36, 0.4);
    --highlight-check: rgba(239, 68, 68, 0.7);
}

html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 50%, #0f172a 100%);
    color: var(--text-primary);
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
    color: inherit;
}

.hidden {
    display: none !important;
}

/* ===== 시작 화면 ===== */
.start-screen {
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
    padding: 1.5rem 1rem 2rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.start-header {
    text-align: center;
    padding: 1rem 0 0.5rem;
}

.start-logo {
    font-size: 3.5rem;
    background: linear-gradient(135deg, var(--gold), var(--accent));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.start-header h1 {
    font-family: 'Cinzel', serif;
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(135deg, #fff, #cbd5e1);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: 1px;
}

.start-header p {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 0.3rem;
}

.start-card {
    background: rgba(30, 41, 59, 0.7);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(99, 102, 241, 0.2);
    border-radius: 16px;
    padding: 1.2rem;
}

.start-card h3 {
    font-size: 0.95rem;
    color: var(--text-secondary);
    margin-bottom: 0.8rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.difficulty-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.diff-btn {
    background: rgba(51, 65, 85, 0.5);
    border: 2px solid transparent;
    border-radius: 12px;
    padding: 0.8rem 0.3rem;
    color: var(--text-primary);
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3rem;
    min-height: 70px;
}

.diff-btn i {
    font-size: 1.3rem;
    color: var(--text-muted);
}

.diff-btn span {
    font-size: 0.8rem;
    font-weight: 600;
}

.diff-btn:active {
    transform: scale(0.95);
}

.diff-btn.active {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    border-color: var(--accent);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.diff-btn.active i {
    color: #fff;
}

.color-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.color-choice {
    background: rgba(51, 65, 85, 0.5);
    border: 2px solid var(--border-color);
    border-radius: 16px;
    padding: 1.2rem 0.5rem;
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3rem;
    transition: all 0.2s ease;
    min-height: 130px;
}

.big-piece-icon {
    font-size: 3.5rem;
    line-height: 1;
}

.white-piece {
    color: #f8fafc;
    text-shadow: 0 0 15px rgba(255, 255, 255, 0.4);
}

.black-piece {
    color: #1e293b;
    text-shadow: 0 0 12px rgba(0, 0, 0, 0.6), 0 0 0 2px #475569;
}

.color-choice strong {
    font-size: 1rem;
    font-weight: 700;
    margin-top: 0.3rem;
}

.color-choice small {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.color-choice:active {
    transform: scale(0.97);
    border-color: var(--primary);
}

.desktop-link {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
    text-decoration: none;
    padding: 0.75rem;
    background: rgba(51, 65, 85, 0.3);
    border-radius: 10px;
    margin-top: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.desktop-link:active {
    background: rgba(99, 102, 241, 0.2);
    color: var(--text-primary);
}

/* ===== 게임 화면 ===== */
.game-screen {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ===== 헤더 ===== */
.mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.75rem;
    padding-top: calc(0.6rem + env(safe-area-inset-top));
    background: rgba(15, 23, 42, 0.95);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid rgba(99, 102, 241, 0.2);
    flex-shrink: 0;
    z-index: 50;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: rgba(51, 65, 85, 0.5);
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: all 0.2s ease;
}

.icon-btn:active {
    background: var(--primary);
    transform: scale(0.92);
}

.header-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-family: 'Cinzel', serif;
    font-size: 1.05rem;
    font-weight: 700;
}

.header-title i {
    color: var(--gold);
}

/* ===== 플레이어 바 ===== */
.player-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 0.75rem;
    background: rgba(30, 41, 59, 0.8);
    border-top: 1px solid rgba(99, 102, 241, 0.15);
    border-bottom: 1px solid rgba(99, 102, 241, 0.15);
    flex-shrink: 0;
    transition: background 0.3s ease;
}

.ai-bar {
    background: linear-gradient(90deg, rgba(239, 68, 68, 0.15), rgba(30, 41, 59, 0.8));
}

.user-bar {
    background: linear-gradient(90deg, rgba(59, 130, 246, 0.15), rgba(30, 41, 59, 0.8));
}

.player-bar.active {
    box-shadow: inset 0 0 0 2px var(--success);
}

.player-bar-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.player-icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    flex-shrink: 0;
}

.user-icon {
    background: linear-gradient(135deg, var(--user-color), #1e40af);
}

.ai-icon {
    background: linear-gradient(135deg, var(--ai-color), #991b1b);
}

.player-bar-info {
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.player-bar-info strong {
    font-size: 0.9rem;
}

.player-bar-info small {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.player-bar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.captured {
    font-size: 1.1rem;
    line-height: 1;
    word-spacing: -4px;
    letter-spacing: -2px;
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.rate-badge {
    font-size: 0.85rem;
    font-weight: 700;
    padding: 0.25rem 0.55rem;
    border-radius: 20px;
    min-width: 50px;
    text-align: center;
}

.user-rate-badge {
    background: rgba(59, 130, 246, 0.25);
    color: #93c5fd;
}

.ai-rate-badge {
    background: rgba(239, 68, 68, 0.25);
    color: #fca5a5;
}

/* ===== 승률 바 ===== */
.win-rate-bar {
    height: 22px;
    display: flex;
    background: var(--bg-medium);
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

.rate-segment {
    height: 100%;
    transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-segment {
    background: linear-gradient(90deg, var(--user-color), #60a5fa);
}

.ai-segment {
    background: linear-gradient(90deg, #f87171, var(--ai-color));
}

.eval-display {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    pointer-events: none;
}

/* ===== 체스 보드 ===== */
.board-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    position: relative;
    min-height: 0;
}

.mobile-board {
    width: 100%;
    max-width: min(100vw - 1rem, 100% - 1rem);
    aspect-ratio: 1;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    margin: 0 auto;
}

.square {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s ease;
}

.square.light {
    background-color: var(--light-square);
}

.square.dark {
    background-color: var(--dark-square);
}

.square.last-move::after {
    content: '';
    position: absolute;
    inset: 0;
    background: var(--highlight-last);
    pointer-events: none;
}

.square.selected {
    background-color: var(--highlight-select) !important;
}

.square.possible-move::before {
    content: '';
    position: absolute;
    width: 30%;
    height: 30%;
    background: var(--highlight-move);
    border-radius: 50%;
    z-index: 1;
}

.square.possible-capture::before {
    content: '';
    position: absolute;
    inset: 6%;
    border: 4px solid var(--highlight-move);
    border-radius: 50%;
    z-index: 1;
    background: transparent;
}

.square.in-check {
    background: radial-gradient(circle, var(--highlight-check), transparent 70%) !important;
}

.square.in-check.light {
    background: radial-gradient(circle, var(--highlight-check), var(--light-square) 80%) !important;
}

.square.in-check.dark {
    background: radial-gradient(circle, var(--highlight-check), var(--dark-square) 80%) !important;
}

.piece {
    width: 92%;
    height: 92%;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    z-index: 2;
    position: relative;
    transition: transform 0.15s ease;
    pointer-events: none;
}

.square.coord-label::after {
    /* placeholder for coordinates if desired */
}

.coord {
    position: absolute;
    font-size: 0.55rem;
    font-weight: 700;
    pointer-events: none;
    z-index: 3;
}

.coord.file {
    bottom: 1px;
    right: 3px;
}

.coord.rank {
    top: 1px;
    left: 3px;
}

.coord.on-light {
    color: var(--dark-square);
}

.coord.on-dark {
    color: var(--light-square);
}

.thinking-overlay {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.75);
    backdrop-filter: blur(4px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    z-index: 20;
    color: #fff;
}

.thinking-overlay i {
    font-size: 2.5rem;
    color: var(--accent);
}

.thinking-overlay p {
    font-size: 1rem;
    font-weight: 600;
}

/* ===== 상태 바 ===== */
.status-bar {
    text-align: center;
    padding: 0.6rem 1rem;
    background: rgba(30, 41, 59, 0.7);
    font-size: 0.9rem;
    font-weight: 500;
    flex-shrink: 0;
    border-top: 1px solid rgba(99, 102, 241, 0.15);
}

.status-bar.check {
    background: rgba(239, 68, 68, 0.25);
    color: #fca5a5;
}

.status-bar.checkmate {
    background: rgba(251, 191, 36, 0.25);
    color: var(--gold);
}

/* ===== 하단 탭 ===== */
.bottom-tabs {
    display: flex;
    background: rgba(15, 23, 42, 0.95);
    border-top: 1px solid rgba(99, 102, 241, 0.2);
    flex-shrink: 0;
}

.tab-btn {
    flex: 1;
    padding: 0.6rem 0.3rem;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.2rem;
    font-size: 0.7rem;
    transition: all 0.2s ease;
    border-top: 2px solid transparent;
}

.tab-btn i {
    font-size: 1.1rem;
}

.tab-btn.active {
    color: var(--primary);
    border-top-color: var(--primary);
    background: rgba(99, 102, 241, 0.08);
}

.tab-btn:active {
    background: rgba(99, 102, 241, 0.15);
}

/* ===== 탭 패널 ===== */
.tab-panel {
    display: none;
    background: rgba(15, 23, 42, 0.95);
    padding: 0.75rem;
    max-height: 30vh;
    overflow-y: auto;
    border-top: 1px solid rgba(99, 102, 241, 0.15);
    flex-shrink: 0;
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
}

.tab-panel.active {
    display: block;
    animation: slideUp 0.25s ease;
}

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

/* 정보 탭 */
.info-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
}

.info-item {
    background: rgba(51, 65, 85, 0.4);
    border-radius: 10px;
    padding: 0.6rem 0.8rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.info-label {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.info-value {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* 기보 탭 */
.mobile-moves-list {
    background: rgba(51, 65, 85, 0.3);
    border-radius: 10px;
    padding: 0.6rem;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    min-height: 80px;
}

.no-moves-msg {
    color: var(--text-muted);
    text-align: center;
    font-style: italic;
    padding: 1rem 0;
}

.move-row {
    display: grid;
    grid-template-columns: 30px 1fr 1fr;
    gap: 0.4rem;
    padding: 0.25rem 0;
    border-bottom: 1px solid rgba(71, 85, 105, 0.3);
}

.move-row:last-child {
    border-bottom: none;
}

.move-num {
    color: var(--text-muted);
    font-weight: 600;
}

.move-white,
.move-black {
    padding: 0.15rem 0.35rem;
    border-radius: 4px;
}

.move-white.last-move-mark,
.move-black.last-move-mark {
    background: rgba(99, 102, 241, 0.3);
}

/* 차트 탭 */
.chart-wrap {
    height: 25vh;
    min-height: 150px;
    background: rgba(51, 65, 85, 0.3);
    border-radius: 10px;
    padding: 0.5rem;
    position: relative;
}

/* 스크롤바 */
.tab-panel::-webkit-scrollbar,
.mobile-moves-list::-webkit-scrollbar,
.start-screen::-webkit-scrollbar {
    width: 4px;
}

.tab-panel::-webkit-scrollbar-thumb,
.mobile-moves-list::-webkit-scrollbar-thumb,
.start-screen::-webkit-scrollbar-thumb {
    background: rgba(99, 102, 241, 0.5);
    border-radius: 2px;
}

/* ===== 바텀 시트 (메뉴) ===== */
.bottom-sheet {
    position: fixed;
    inset: 0;
    z-index: 200;
}

.sheet-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    animation: fadeIn 0.2s ease;
}

.sheet-content {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(180deg, #1e293b, #0f172a);
    border-radius: 20px 20px 0 0;
    border-top: 1px solid rgba(99, 102, 241, 0.3);
    padding: 0.75rem 1rem 1.5rem;
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
    animation: slideUpSheet 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes slideUpSheet {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.sheet-handle {
    width: 40px;
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
    margin: 0 auto 1rem;
}

.sheet-content h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--text-secondary);
    text-align: center;
}

.sheet-btn {
    width: 100%;
    padding: 1rem;
    background: rgba(51, 65, 85, 0.5);
    border-radius: 12px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.95rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
    transition: all 0.2s ease;
}

.sheet-btn i {
    width: 24px;
    text-align: center;
    color: var(--primary);
}

.sheet-btn.danger i {
    color: var(--danger);
}

.sheet-btn:active {
    background: rgba(99, 102, 241, 0.25);
    transform: scale(0.98);
}

.sheet-btn.danger:active {
    background: rgba(239, 68, 68, 0.25);
}

/* ===== 모달 ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
    padding: 1rem;
    animation: fadeIn 0.25s ease;
}

.modal-box {
    background: linear-gradient(135deg, #1e293b, #0f172a);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 20px;
    padding: 1.75rem 1.25rem;
    max-width: 360px;
    width: 100%;
    text-align: center;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    animation: scaleIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes scaleIn {
    from { opacity: 0; transform: scale(0.85); }
    to { opacity: 1; transform: scale(1); }
}

.modal-box h2,
.modal-box h3 {
    font-family: 'Cinzel', serif;
    margin-bottom: 0.4rem;
}

.modal-box h3 {
    font-size: 1.1rem;
}

.modal-box p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1.25rem;
}

.promo-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
}

.promo-btn {
    background: rgba(51, 65, 85, 0.5);
    border: 2px solid var(--border-color);
    border-radius: 12px;
    padding: 0.7rem 0.3rem;
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.2rem;
    transition: all 0.2s ease;
}

.promo-piece {
    font-size: 2rem;
    line-height: 1;
}

.promo-btn small {
    font-size: 0.7rem;
}

.promo-btn:active {
    background: rgba(99, 102, 241, 0.3);
    border-color: var(--primary);
    transform: scale(0.95);
}

/* 결과 모달 */
.result-box {
    padding: 2rem 1.25rem;
}

.big-result-icon {
    font-size: 3.5rem;
    margin-bottom: 0.75rem;
}

.big-result-icon.win {
    color: var(--gold);
    animation: bounce 1s ease;
}

.big-result-icon.lose {
    color: var(--text-muted);
}

.big-result-icon.draw {
    color: var(--primary);
}

@keyframes bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

#result-title {
    font-size: 1.6rem;
    margin-bottom: 0.4rem;
}

.result-info {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin: 1rem 0 1.25rem;
    padding: 0.9rem;
    background: rgba(51, 65, 85, 0.3);
    border-radius: 12px;
}

.result-info-item {
    text-align: center;
}

.result-info-item small {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 0.3rem;
}

.result-info-item strong {
    display: block;
    font-size: 1.3rem;
    color: var(--gold);
}

.modal-actions {
    display: flex;
    gap: 0.5rem;
}

.action-btn {
    flex: 1;
    padding: 0.75rem 0.5rem;
    background: rgba(51, 65, 85, 0.5);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: 0.85rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    transition: all 0.2s ease;
}

.action-btn:active {
    transform: scale(0.96);
}

.action-btn.primary {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
}

/* ===== 작은 화면 대응 ===== */
@media (max-height: 700px) {
    .player-bar {
        padding: 0.35rem 0.75rem;
    }
    .player-icon {
        width: 30px;
        height: 30px;
        font-size: 0.85rem;
    }
    .win-rate-bar {
        height: 18px;
    }
    .eval-display {
        font-size: 0.7rem;
    }
    .status-bar {
        padding: 0.4rem 1rem;
        font-size: 0.85rem;
    }
    .tab-btn {
        padding: 0.4rem 0.3rem;
    }
    .tab-btn i {
        font-size: 1rem;
    }
}

@media (max-height: 600px) {
    .tab-panel {
        max-height: 25vh;
    }
}

/* 가로 모드 경고 */
@media (orientation: landscape) and (max-height: 500px) {
    .game-screen::before {
        content: '📱 세로 모드를 권장합니다';
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: rgba(15, 23, 42, 0.95);
        color: var(--gold);
        padding: 1rem 1.5rem;
        border-radius: 12px;
        z-index: 1000;
        font-size: 0.9rem;
        border: 1px solid var(--gold);
        white-space: nowrap;
    }
}

/* 매우 작은 화면 */
@media (max-width: 360px) {
    .difficulty-grid {
        gap: 0.4rem;
    }
    .diff-btn {
        padding: 0.6rem 0.2rem;
        min-height: 60px;
    }
    .diff-btn span {
        font-size: 0.7rem;
    }
    .start-header h1 {
        font-size: 1.7rem;
    }
}
