/* --- Root Variables & Reset --- */
:root {
    --bg: #121213;
    --tile: #3a3a3c;
    --tile-hover: #4a4a4c;
    --text: #ffffff;
    --accent: #538d4e;
    --error: #b51515;
    --fixed-text: #818384;
    --exponent-bg: #2c2c2e;
    --exponent-text: #aaaaaa;
    --shadow: 0 4px 0 #1a1a1a;
    --transition: 0.15s ease-in-out;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100dvh; /* Dynamic viewport height to prevent mobile scroll issues */
    width: 100vw;
    margin: 0;
    padding: 0;
    overflow: hidden; /* Strictly prevents scrolling on the main page */
}

body {
    background-color: var(--bg);
    color: var(--text);
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    display: flex;
    justify-content: center;
    padding: 1dvh 15px; /* Scaled padding */
    user-select: none;
}

.main-container {
    position: relative;
    width: 100%;
    max-width: 450px;
    height: 100%; /* Takes full constrained height */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between; /* Distributes elements evenly */
}

/* --- Header & Stats --- */
.header {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 1dvh 0;
    flex-shrink: 0;
}

h1 {
    font-size: clamp(1.8rem, 5dvh, 2.5rem); /* Fluid font size */
    font-weight: 800;
    margin: 0;
    letter-spacing: 4px;
    text-transform: uppercase;
}

.stats {
    font-size: clamp(0.75rem, 2dvh, 0.85rem);
    color: var(--fixed-text);
    display: flex;
    justify-content: center;
    gap: 25px;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-top: 1dvh;
    width: 100%;
}

/* --- Active Word Area --- */
#word-area {
    display: flex;
    gap: clamp(6px, 2vw, 12px);
    margin: 1.5dvh 0;
    flex-shrink: 0;
}

.slot {
    width: min(16vw, 62px); /* Scales down on narrow screens */
    height: min(16vw, 62px);
    border: 2px solid var(--tile);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.5rem, 6vw, 2.2rem);
    font-weight: 700;
    text-transform: uppercase;
    border-radius: 4px;
    transition: transform var(--transition), border-color var(--transition);
}

.slot.blank {
    border-color: var(--accent);
    cursor: pointer;
}

.slot.filled {
    background-color: var(--accent);
    border-color: var(--accent);
    animation: popIn 0.1s ease-out;
}

.slot.fixed {
    background-color: var(--tile);
    color: var(--fixed-text);
    border-color: transparent;
}

/* --- Tray Sections --- */
.tray-section {
    width: 100%;
    margin-bottom: 1.5dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 1; /* Allows shrinking to fit */
}

.section-label {
    font-size: clamp(0.55rem, 1.5dvh, 0.65rem);
    color: #555;
    text-transform: uppercase;
    letter-spacing: 2.5px;
    margin-bottom: 1dvh;
    text-align: center;
}

.tray-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: min(2vw, 14px);
    width: 100%;
}

/* --- Individual Tiles --- */
.tile-wrapper {
    position: relative;
    width: min(12vw, 52px);
    height: min(12vw, 52px);
}

.tile {
    width: 100%;
    height: 100%;
    background: var(--tile);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.1rem, 5vw, 1.5rem);
    font-weight: 700;
    border-radius: 8px;
    cursor: pointer;
    box-shadow: var(--shadow);
    transition: transform 0.05s, background-color var(--transition);
}

.tile:hover {
    background-color: var(--tile-hover);
}

.tile:active, .tile.pulse {
    transform: translateY(3px);
    box-shadow: 0 1px 0 #1a1a1a;
}

.tile.empty {
    background: transparent;
    border: 2px dashed #2c2c2e;
    box-shadow: none;
    cursor: default;
    color: transparent;
}

.count {
    position: absolute;
    top: -6px;
    right: -6px;
    background: var(--exponent-bg);
    color: var(--exponent-text);
    font-size: clamp(0.6rem, 2vw, 0.7rem);
    padding: 2px 5px;
    border-radius: 6px;
    font-weight: 800;
    border: 2px solid var(--bg);
    pointer-events: none;
}

/* --- Messaging & Controls --- */
#message {
    height: 20px;
    line-height: 20px;
    font-weight: 700;
    text-align: center;
    margin: 1dvh 0;
    font-size: 0.9rem;
    letter-spacing: 1px;
    flex-shrink: 0;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 1dvh;
    width: 100%;
    max-width: 380px;
    padding-bottom: 2dvh; /* Scaled padding to keep it off the very bottom */
    flex-shrink: 0;
}

.row {
    display: flex;
    gap: 10px;
}

button {
    padding: clamp(12px, 2.5dvh, 18px);
    border: none;
    border-radius: 12px;
    font-weight: 700;
    cursor: pointer;
    font-size: clamp(0.9rem, 2.5dvh, 1.1rem);
    color: white;
    transition: opacity 0.2s, transform 0.1s;
}

button:active {
    transform: scale(0.98);
}

.btn-submit { background: var(--accent); flex: 2; }
.btn-clear { background: var(--tile); flex: 1; }
.btn-giveup {
    background: transparent;
    color: #555;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 0.5dvh;
    padding: 10px;
}

/* --- Results Screen (Mobile Scrollable inside modal only) --- */
#game-over {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--bg);
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    z-index: 1000;
    padding: 5dvh 20px;
    overflow-y: auto; /* Results screen STILL needs scrolling because of dynamic global charts */
    -webkit-overflow-scrolling: touch;
    animation: fadeIn 0.4s ease-out;
}

#game-over h2 {
    margin-top: 0;
    margin-bottom: 2dvh;
    flex-shrink: 0;
}

.results-summary-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4vw;
    margin-bottom: 3dvh;
    flex-shrink: 0;
}

/* Progress Ring */
.score-ring-container {
    position: relative;
    width: 140px;
    height: 140px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.progress-ring {
    position: absolute;
    transform: rotate(-90deg);
    transform-origin: 50% 50%;
}

.progress-ring__bar {
    transition: stroke-dashoffset 1.5s ease-out;
    stroke-linecap: round;
    stroke-dasharray: 400; 
    stroke-dashoffset: 400; 
}

.score-text {
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 10;
}

.score-text span { font-size: 3.5rem; font-weight: 900; line-height: 1; color: var(--text); }
.score-text label { font-size: 0.7rem; text-transform: uppercase; color: var(--fixed-text); letter-spacing: 2px; }

/* Metrics Column */
.metrics-column {
    display: flex;
    flex-direction: column;
    gap: 15px;
    border-left: 2px solid var(--tile);
    padding-left: 20px;
    flex-shrink: 0;
}

.metric-box label { font-size: 0.6rem; text-transform: uppercase; color: var(--fixed-text); letter-spacing: 1px; display: block; }
.metric-box span { font-size: 1.8rem; font-weight: 800; color: var(--accent); }

/* --- Global Stats (Floating Bar Chart) --- */
#global-stats-container {
    width: 100%;
    max-width: 320px;
    margin: 0 auto 3dvh auto;
    text-align: center;
    flex-shrink: 0;
    background: transparent;
}

#global-stats-container h3 {
    font-size: 0.7rem;
    color: var(--fixed-text);
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 15px;
}

#stats-graph.vertical-chart {
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    justify-content: center;
    min-height: 140px; 
    height: 140px; 
    width: 100%;
    padding: 20px 0 10px 0;
    gap: 4px;
    box-sizing: border-box;
}

.stats-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%; 
    justify-content: flex-end; 
}

.stats-bar-wrapper {
    width: 100%;
    height: 100%; 
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    position: relative; 
}

.stats-bar {
    width: 100%;
    background: var(--tile);
    border-radius: 2px 2px 0 0;
    transition: height 1s ease-out;
    min-height: 2px; 
    position: relative;
}

.stats-bar.highlight { background: var(--accent); }

.bar-value {
    font-size: clamp(0.45rem, 2vw, 0.55rem);
    color: var(--fixed-text);
    position: absolute;
    top: -16px;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
    font-weight: bold;
}

.stats-label {
    font-size: 0.65rem;
    margin-top: 8px;
    color: var(--fixed-text);
    font-weight: bold;
    width: 100%;
    text-align: center;
}

/* --- Final Word List --- */
#final-word-list-container {
    width: 100%;
    max-width: 320px;
    background: #1a1a1b;
    padding: 20px;
    border-radius: 12px;
    border: 1px solid var(--tile);
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 3dvh;
    flex-shrink: 0;
}

#final-word-list-container h3 {
    font-size: 0.7rem;
    color: var(--fixed-text);
    margin: 0 0 15px 0;
    letter-spacing: 1px;
}

#final-word-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    max-height: 140px;
    overflow-y: auto;
    justify-content: center;
    margin-bottom: 15px;
    width: 100%;
}

#final-word-list span {
    background: var(--tile);
    color: var(--text);
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
}

#final-word-list::-webkit-scrollbar { width: 4px; }
#final-word-list::-webkit-scrollbar-thumb { background: var(--tile); border-radius: 10px; }

/* --- Footer Buttons --- */
.over-btn { width: 100%; max-width: 250px; margin: 5px 0; flex-shrink: 0; }
.small-btn { font-size: 0.75rem; background: #3a3a3c; width: 220px; }
.blue-btn { background: #1a73e8; }
.green-btn { background: var(--accent); }

/* --- History Log (Main Game Page) --- */
#history-log {
    display: flex;
    overflow-x: auto;
    width: 100%;
    max-width: 500px;
    gap: 8px;
    padding: 1dvh 0 1.5dvh 0;
    margin-bottom: 1dvh;
    scrollbar-width: thin;
    scrollbar-color: var(--tile) transparent;
}

#history-log::-webkit-scrollbar { display: none; /* Hide scrollbar Chrome/Safari */ }

#history-log::-webkit-scrollbar { 
    height: 6px; 
}

#history-log::-webkit-scrollbar-track { 
    background: transparent; 
}

#history-log::-webkit-scrollbar-thumb { 
    background-color: var(--tile); 
    border-radius: 10px; 
}

.log-entry {
    background: #272729;
    color: var(--fixed-text);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: clamp(0.7rem, 2vw, 0.8rem);
    white-space: nowrap;
    cursor: pointer;
    border: 1px solid #333;
    flex-shrink: 0;
}

/* --- Custom Modal Styling --- */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    z-index: 2000;
    justify-content: center;
    align-items: center;
    padding: 20px;
    animation: fadeIn 0.2s ease-out;
}

.modal-content {
    background: var(--bg);
    border: 1px solid var(--tile);
    width: 90%;       
    max-width: 360px; 
    padding: 25px;
    border-radius: 16px;
    text-align: center;
}

#modal-title {
    font-size: 1.8rem;
    margin: 0 0 15px 0;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: var(--accent);
}

#modal-body {
    font-size: 1rem;
    line-height: 1.5;
    color: var(--text);
    margin-bottom: 25px;
}

.modal-footer {
    display: flex;
    gap: 12px;
    width: 100%;
    justify-content: center;
    margin-top: 25px;
}

.modal-footer .over-btn {
    width: auto;      
    flex: 1;          
    min-width: 0;     
    max-width: 140px; 
    margin: 0;        
    padding: 12px 5px;
    font-size: 0.9rem;
}

#modal-cancel-btn {
    background: var(--tile);
}

.icon-btn {
    position: absolute;
    top: 1dvh;
    right: 0;
    background: var(--tile);
    color: var(--fixed-text);
    border: none;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    font-family: serif;
    font-style: italic;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 101; 
}

.icon-btn:hover {
    background: var(--tile-hover);
    color: var(--text);
}

/* --- Support/Donate Section --- */
.support-link {
    display: block;
    margin-top: 20px;
    padding-top: 15px;
    border-top: 1px solid var(--tile);
    color: var(--accent);
    text-decoration: none;
    font-weight: bold;
    font-size: 0.9rem;
}

#support-dev-btn {
    background: linear-gradient(to right, #555 20%, #999 50%, #555 80%);
    background-size: 200% auto;
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: shimmer-ltr 4s linear infinite;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 15px;
    cursor: pointer;
    font-weight: bold;
    border: none;
    display: inline-block;
    background-color: transparent;
}

/* --- Animations --- */
@keyframes popIn {
    0% { transform: scale(0.8); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes shimmer-ltr {
    0% { background-position: 200% center; }
    100% { background-position: -200% center; }
}

#support-dev-btn:hover {
    -webkit-text-fill-color: var(--accent);
    transition: var(--transition);
}

/* --- Pulse Animation for Review Mode --- */
@keyframes pulse-accent {
    0% { box-shadow: 0 0 0 0 rgba(83, 141, 78, 0.7); }
    70% { box-shadow: 0 0 0 10px rgba(83, 141, 78, 0); }
    100% { box-shadow: 0 0 0 0 rgba(83, 141, 78, 0); }
}

.pulse-btn {
    animation: pulse-accent 2s infinite !important;
}

/* --- Differentiated Letters in History Log --- */
.log-entry span {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 1.5em;      /* Strict width so I and W are identical */
    height: 1.5em;     /* Strict height to make a perfect square */
    border-radius: 0.2em;
    margin: 0 0.1em;
    font-weight: 600;  /* Slightly bold makes the small tiles readable */
    pointer-events: none; /* Inherited from our earlier fix */
}

/* --- Specific Colors --- */
.log-entry .user-letter {
    background-color: var(--accent);
    color: var(--text);
}

.log-entry .pattern-letter {
    background-color: var(--tile);
    color: var(--text);
}